2 Day Developer > Using PL/SQL > Handling PL/SQL Errors > Scope Rules for PL/SQL Exce...
Scope Rules for PL/SQL Exceptions |
Previous |
Next |
You cannot declare an exception twice in the same block. You can, however, declare the same exception in two different blocks.
Exceptions declared in a block are considered local to that block and global to all its subblocks. Because a block can reference only local or global exceptions, enclosing blocks cannot reference exceptions declared in a subblock.
If you redeclare a global exception in a subblock, the local declaration prevails. The subblock cannot reference the global exception, unless the exception is declared in a labeled block and you qualify its name with the block label, for example:
block_label.exception_name
Example: Determining the Scope of PL/SQL Exceptions shows the scope rules.
Determining the Scope of PL/SQL Exceptions
DECLARE past_due EXCEPTION; acct_num NUMBER; BEGIN DECLARE ---------- subblock begins past_due EXCEPTION; -- this declaration prevails acct_num NUMBER; due_date DATE := SYSDATE - 1; -- set on purpose to raise exception todays_date DATE := SYSDATE; BEGIN IF due_date < todays_date THEN RAISE past_due; -- this is not handled END IF; END; ------------- subblock ends EXCEPTION WHEN past_due THEN -- does not handle raised exception DBMS_OUTPUT.PUT_LINE('Handling PAST_DUE exception.'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Could not recognize PAST_DUE_EXCEPTION in this scope.'); END; /
The enclosing block does not handle the raised exception because the declaration of past_due
in the subblock prevails. Although they share the same name, the two past_due
exceptions are different, just as the two acct_num
variables share the same name but are different variables. Thus, the RAISE
statement and the WHEN
clause refer to different exceptions. To have the enclosing block handle the raised exception, you must remove its declaration from the subblock or define an OTHERS
handler.