Professional Documents
Culture Documents
Slide 1-1
EXCEPTION-HANDLING SECTION
▪The exception-handling section is the last section of the PL/SQL
block.
▪An exception is a PL/SQL error that is raised during program
execution, either implicitly or explicitly by your program.
▪Handle an exception by trapping it with a handler or propagating it to
the calling environment.
▪There are two categories of exceptions in the world of PL/SQL:
predefined and user-defined.
➢A predefined exception is an internally defined exception that is
assigned a name by PL/SQL.
➢A user-defined exception is one you have declared in the
declaration section of a program unit. User-defined exceptions can
be associated with an internally defined exception (that is, you can
give a name to an otherwise unnamed exception) or with an
application-specific error.
EXCEPTION-HANDLING SECTION
For example, if your SELECT statement returns multiple rows, then
Oracle returns an error (exception) at runtime.
DECLARE
> v_lname VARCHAR2 (15);
> BEGIN
> SELECT last_name INTO v_lname
> FROM employees
> WHERE first_name = 'John';
> DBMS_OUTPUT.PUT_LINE ('Last name is :' || v_lname);
> END;
> /
8507: ORA-01422: exact fetch returns more than requested number of
rows
Slide 2-
3
EXCEPTION-HANDLING SECTION
You can handle such exceptions in your PL/SQL block so that your program
completes successfully. For example:
Command> DECLARE
> v_lname VARCHAR2 (15);
> BEGIN
> SELECT last_name INTO v_lname
> FROM employees
> WHERE first_name = 'John';
> DBMS_OUTPUT.PUT_LINE ('Last name is :' || v_lname);
> EXCEPTION
> WHEN TOO_MANY_ROWS THEN
> DBMS_OUTPUT.PUT_LINE (' Your SELECT statement retrieved multiple
> rows. Consider using a cursor.');
> END;
Your SELECT statement retrieved multiple rows. Consider using a cursor.
Slide 2-
4
TRAPPING USER-DEFINED EXCEPTIONS
DECLARE
> BEGIN
> UPDATE departments
> ROLLBACK;
> EXCEPTION
> END;
No such department
Slide 2-
5
SOME PREDEFINED EXCEPTIONS
CURSOR_ALREADY_OPEN ORA-06511 6511 Program attempted to open an already
ED opened cursor.