PL/SQL Exception Handling Guide
PL/SQL Exception Handling Guide
PL/SQL facilitates programmers to catch such conditions using exception block in the
program and an appropriate action is taken against the error condition.
DECLARE
declarations section;
BEGIN
executable command(s);
EXCEPTION
WHEN exception1 THEN
statement1;
WHEN exception2 THEN
statement2;
[WHEN others THEN]
/* default exception handling code */
END;
Exception Description
DECLARE
a INT:=10;
b INT:=0;
res INT;
BEGIN
res:=a/b;
DBMS_OUTPUT.PUT_LINE('Result is: ' || res);
NULL;
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Divide by zero is not possible');
END;
/
Output:
Statement processed.
Divide by zero is not possible
DECLARE
sname [Link]%TYPE;
BEGIN
SELECT name
INTO sname
FROM student WHERE sid=103;
DBMS_OUTPUT.PUT_LINE('Name:’ || sname);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data to display');
END;
/
Output:
Statement processed.
No data to display
TOO_MANY_ROWS:
It is raised WHEN a SELECT INTO statement returns more than one row.
DECLARE
sname [Link]%TYPE;
BEGIN
SELECT name
INTO sname
FROM student;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data to display');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Too many rows selected.');
END;
/
Output:
Statement processed.
Too many rows selected.
Example -1
DECLARE
a NUMBER; --declare a as number
BEGIN
a:='Ram'; --putting string in a
EXCEPTION
WHEN VALUE_ERROR THEN
DBMS_OUTPUT.PUT_LINE('Invalid value to number type');
END;
/
Output:
Statement processed.
Invalid value to number type
Example – 2
DECLARE
sname NUMBER; --number?
BEGIN
SELECT name
INTO sname
FROM student WHERE sid=102;
EXCEPTION
WHEN VALUE_ERROR THEN
DBMS_OUTPUT.PUT_LINE('Invalid value to store');
END;
/
Output:
Statement processed.
Invalid value to store
Syntax:
DECLARE
exception_name EXCEPTION;
BEGIN ...
IF CONDITION THEN
RAISE exception_name;
ELSE ...
END IF;
EXCEPTION
END;
DECLARE
age NUMBER:=&age; --taking value at runtime
invalid_age EXCEPTION; --declaring exception
BEGIN
--condition for exception
IF age<18 THEN
RAISE invalid_age;
ELSE
DBMS_OUTPUT.PUT_LINE('Eligible to vote.');
END IF;
EXCEPTION
WHEN invalid_age THEN --checking exception
DBMS_OUTPUT.PUT_LINE('Exception - Not Eligible to vote.');
END;
/
Output:
If age = 20 then,
Statement processed.
Eligible to vote.
If age = 16 then,
Statement processed.
Exception - Not Eligible to vote.
DECLARE
num INT;
invalid_num EXCEPTION;
BEGIN
DECLARE
invalid_num EXCEPTION;
BEGIN
num:=-1;
IF num<0 THEN
RAISE invalid_num;
END IF;
EXCEPTION
WHEN invalid_num THEN
DBMS_OUTPUT.PUT_LINE('Exception-Invalid Number-Must be
Positive');
END;
EXCEPTION
WHEN invalid_num THEN
DBMS_OUTPUT.PUT_LINE('Exception-Invalid Number-Must be
Odd');
END;
/
Output:
Statement processed.
Exception-Invalid Number-Must be Positive
Exception-Invalid Number-Must be Odd
DECLARE
num INT;
invalid_num EXCEPTION;
BEGIN
BEGIN
num:=-1;
IF num<0 THEN
RAISE invalid_num;
END IF;
EXCEPTION
WHEN invalid_num THEN
DBMS_OUTPUT.PUT_LINE('Exception-Invalid Number-Must be
Positive');
END;
END;
/
Output:
Statement processed.
Exception-Invalid Number-Must be Positive
DECLARE
num INT;
BEGIN
DECLARE
invalid_num EXCEPTION;
BEGIN
num:=-1;
IF num<0 THEN
RAISE invalid_num;
-- this can't be referenced by outer block
-- this can't be handeled
END IF;
END;
EXCEPTION
WHEN invalid_num THEN
DBMS_OUTPUT.PUT_LINE('Exception-Invalid Number-Must be Positive');
END;
/
Output:
ORA-06550: line 16, column 13:
PLS-00201: identifier 'INVALID_NUM' must be declared
This process continues in each successive enclosing block until there is no remaining block
in which to raise the exception.
If there is no exception handler in all blocks, PL/SQL returns an unhandled exception to the
application environment that executed the outermost PL/SQL block.
Example -1
A runtime error occurs in the declaration section of the block. If there is no outer block,
execution of the program halts, and control is passed to the host environment. Consider
the following script:
DECLARE
num INT:='Ram'; --exception at declaration block
BEGIN
DBMS_OUTPUT.PUT_LINE(num);
EXCEPTION
WHEN VALUE_ERROR THEN
DBMS_OUTPUT.PUT_LINE('Exception has been occured !');
END;
/
Output:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 2
As you can see, the assignment statement in the declaration section of the block causes an
error. Even though there is an exception handler for this error, the block cannot execute
successfully. Based on this example, you may conclude that when a runtime error occurs in
the declaration section of the PL/SQL block, the exception-handling section of this block
cannot catch the error.
Example – 2
Consider next example with nested blocks, having error in declaration section of inner block.
In this case exception is propagated to outer block and if outer block has exception handler
then exception can be handled.
DECLARE
num INT:='Ram';
Output:
Statement processed.
Exception has been occured - Outer!
In this example, the PL/SQL block is enclosed in another block, and the program can complete.
This is possible because the exception defined in the outer block is raised when the error
occurs in the declaration section of the inner block. Therefore, you can conclude that when
a runtime error occurs in the declaration section of the inner block, the exception
immediately propagates to the enclosing (outer) block.
Example – 3
Consider another example with nested blocks having exception in inner block. If there is no
exception handler in inner block exception is propagated to outer block.
BEGIN
DECLARE
num INT;
BEGIN
num:='Ram'; --Exception
DBMS_OUTPUT.PUT_LINE(num);
END;
Output:
Statement processed.
Exception has been occured!
--Outer Block
DECLARE
exception1 EXCEPTION;
BEGIN
--Inner Block
BEGIN
RAISE exception1;
EXCEPTION
WHEN exception1 THEN
RAISE exception1;
END;
EXCEPTION
WHEN exception1 THEN
DBMS_OUTPUT.PUT_LINE('Exception has occured !');
END;
/
Output:
Statement processed.
Exception has occured !
Syntax:
RAISE_APPLICATION_ERROR(error_number, error_message);
When RAISE_APPLICATION_ERROR executes it returns error message and error code which
looks same as Oracle built-in error.
error_number are pre-defined and have negative integer range from -20000 to -20999.
DECLARE
myexp1 EXCEPTION;
age INT:=17;
BEGIN
IF age<18 THEN
RAISE myexp1;
END IF;
EXCEPTION
WHEN myexp1 THEN
RAISE_APPLICATION_ERROR(-20111,'Exception-Not Eligible !');
END;
/
Output:
ORA-20111: Exception-Not Eligible ! ORA-06512: at line 10
Exception_INIT Pragma
The EXCEPTION_INIT pragma allows you to associate an Oracle error number with the name
of a user-defined error. After you associate an error name with an Oracle error number, you
can reference the error and write a handler for it.
DECLARE
exception_name EXCEPTION;
PRAGMA EXCEPTION_INIT(exception_name, error_code);
Notice that the declaration of the user-defined exception appears before the EXCEPTION_INIT
pragma where it is used. The EXCEPTION_INIT pragma has two parameters: exception_ name
and error_code. exception_name is the name of your exception, and error_code is the
number of the Oracle error you want to associate with your exception.
error_number are pre-defined and have negative integer range from -20000 to -20999.
Output:
Statement processed.
Exception - Not Eligible !
Example - 2
DECLARE
myexp1 EXCEPTION;
PRAGMA EXCEPTION_INIT(myexp1,-20015);
BEGIN
FOR i IN 1..5
LOOP
DBMS_OUTPUT.PUT_LINE(i);
IF i=3 THEN
RAISE myexp1;
END IF;
END LOOP;
EXCEPTION
WHEN myexp1 THEN
DBMS_OUTPUT.PUT_LINE('Exception has been raised!');
END;
/
Output:
Statement processed.
1
2
3
Exception has been raised!