0% found this document useful (0 votes)
19 views

Exception Handling in PLSQL

Uploaded by

Dhavan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Exception Handling in PLSQL

Uploaded by

Dhavan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Exception Handling in PL/SQL:

* It allows you to manage runtime errors or exceptions


* These exceptions may occur during the execution of your code.

* It ensures that your PL/SQL program can handle unexpected situations and
provides error messages or executing alternative logic when an error occurs.

Types of Exceptions:
--------------------
Predefined Exceptions:
----------------------
Oracle provides a set of predefined exceptions for common errors, such as
NO_DATA_FOUND or ZERO_DIVIDE.

User-Defined Exceptions:
-------------------------

You can define your own exceptions for specific error conditions that are not
covered by predefined exceptions.

Unnamed (Non-predefined) Exceptions:


------------------------------------
These are exceptions that are not predefined and do not have names, typically
identified by Oracle error codes.

Common Predefined Exceptions


----------------------------
NO_DATA_FOUND: Raised when a SELECT INTO statement does not return any rows.

TOO_MANY_ROWS: Raised when a SELECT INTO statement returns more than one row.

ZERO_DIVIDE: Raised when an attempt is made to divide a number by zero.

INVALID_CURSOR: Raised when an illegal operation is attempted on a cursor.

Syntax to handle exceptions:

BEGIN
-- Executable statements
EXCEPTION
WHEN exception_name_1 THEN
-- Handling code for exception 1
WHEN exception_name_2 THEN
-- Handling code for exception 2
WHEN OTHERS THEN
-- Handling code for all other exceptions
END;

WHEN OTHERS Clause


------------------
The WHEN OTHERS clause is a catch-all exception handler that captures any
exceptions not explicitly handled by other WHEN clauses.
It should generally be used at the end of the EXCEPTION block to ensure that all
exceptions are managed.

ex:
declare
salary number;
begin
select salary into salary from emp where deptid=12;
dbms_output.put_line(salary);
exception
when NO_DATA_FOUND then
dbms_output.put_line('No salary found for given deptid');
end;
/

declare
salary number;
begin
select salary into salary from emp where deptid=11;
dbms_output.put_line(salary);
exception
when NO_DATA_FOUND then
dbms_output.put_line('No salary found for given deptid');
when TOO_MANY_ROWS then
dbms_output.put_line('Too many rows for given Deptid');
end;
/

declare
salary number;
did number;
begin
did:=&deptid;
select salary into salary from emp where deptid=did;
dbms_output.put_line(salary/0);
exception
when NO_DATA_FOUND then
dbms_output.put_line('No salary found for given deptid');
when TOO_MANY_ROWS then
dbms_output.put_line('Too many rows for given Deptid');
when OTHERS then
dbms_output.put_line('PLSQL Exception found');
end;
/

RAISE_APPLICATION_ERROR() -> Built-in procedure of Oracle


-> raise an error like system defined
syntax:
RAISE_APPLICATION_ERROR(errorNo,'message');
ex:
declare
x number;
y number;
begin
x:=&x;
y:=&y;
if y=0 then
RAISE_APPLICATION_ERROR(-20001,'Division should not be 0');
end if;
dbms_output.put_line(x/y);
end;
/

User-defined exceptions:
* variable must be exception type to become an exception.
ex:
declare
INVALID_SALARY exception;
salary number;
begin
salary:=&salary;
if salary<15000 then
raise INVALID_SALARY;
else
dbms_output.put_line('you can apply for a loan');
end if;
exception
when INVALID_SALARY then
RAISE_APPLICATION_ERROR(-20002,'salary must be above 15000');
end;
/

raise -> keyword used to raise an user defined or predefined exception manually.
syntax:
raise exception_name;

You might also like