Exceptions are said to be error conditions during a program execution. Programmers use EXCEPTIONS to catch these error conditions in the program. There are two different kinds of exceptions:
- System-defined Exceptions
- User-defined Exceptions
Syntax for exception handling:
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling goes here >
WHEN exception1 THEN
exception1-handling-statements
WHEN exception2 THEN
exception2-handling-statements
WHEN exception3 THEN
exception3-handling-statements
........
WHEN others THEN
exception3-handling-statements
END;
Raising Exceptions
Database server raises the exceptions automatically whenever, there is an internal database error, but these exceptions are raised explicitly by the programmer by using the command RAISE.
Syntax:
DECLARE
exception_name EXCEPTION;
BEGIN
IF condition THEN
RAISE exception_name;
END IF;
EXCEPTION
WHEN exception_name THEN
statement;
END;
Syntax:
DECLARE
my_exception EXCEPTION;
Example:
DECLARE
t_id teachers.id%type := &tt_id;
t_name teacherS.Name%type;
t_addr teachers.address%type;
-- user defined exception
ex_invalid_id EXCEPTION;
BEGIN
IF t_id <= 0 THEN
RAISE ex_invalid_id;
ELSE
SELECT name, address INTO t_name, t_addr
FROM teachers
WHERE id = t_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| t_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || t_addr);
END IF;
EXCEPTION
WHEN ex_invalid_id THEN
dbms_output.put_line('ID must be greater than zero!');
WHEN no_data_found THEN
dbms_output.put_line('No such teacher!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/
Output:
Enter value for tt_id: 0 (let's enter a value 0)
old 2: t_id teachers.id%type := &tt_id;
new 2: t_id teachers.id%type := 0;
ID must be greater than zero!
PL/SQL procedure successfully completed.
PRE-DEFINED EXCEPTIONS
There are pre-defined exceptions, that are executed when any of the database rules are violated by a program.
Exception |
Oracle error |
SQL code |
Description |
ACCESS_INTO_NULL |
06530 |
-6530 |
Raised when the null object is automatically assigned a value. |
CASE_NOT_FOUND |
06592 |
-6592 |
This is raised when none of the choices in the WHEN clause of a CASE statement is selected, and there is also no else clause. |
COLLECTIONS_IS_NULL |
06531 |
-6531 |
This is raised when the program attempt for applying collections methods other than exists to an uninitialized nested table of VARRAY. |
DUP_VAL_ON_INDEX |
00001 |
-1 |
This is raised when the duplicate elements are attempted for storing in the column with the unique index. |
INVALID_CURSOR |
01001 |
-1001 |
This is raised when attempts are made for making a cursor operation which is not allowed. |
INVALID_NUMBER |
01722 |
-1722 |
This is raised when the conversion of character string to number fails. |
LOGIN_DENIED |
01017 |
-1017 |
This raises when the program attempts to login with invalid username and password. |
NO_DATA_FOUND |
01403 |
+100 |
When SELECTINTO statement returns no rows, this is raised. |
NOT_LOGGED_ON |
01012 |
-1012 |
Raised when the database issue call is made. |
PROGRAM_ERROR |
06501 |
-6501 |
This is raised due to internal problem in PL/SQL. |
ROWTYPE_MISMATCH |
06504 |
-6504 |
If the cursor fetches a value in a variable that is having incompatible data type. |
SELF_IS_NULL |
30625 |
-30625 |
When a member method is invoked, it is raised. |
STORAGE_ERROR |
06500 |
-6500 |
If the memory is corrupted, or PL/SQL is running out of the program it is raised. |
TOO_MANY_ERRORS |
01422 |
-1422 |
This is raised when the SELECTINTO statement returns more than one row. |
VALUE_ERROR |
06502 |
-6502 |
Raised due to arithmetic, conversion or truncate errors. |
ZERO_DIVIDE |
01476 |
1476 |
Raised when we divide a number by zero. |