12.07.2015 Views

Oracle SQL Developer

Oracle SQL Developer

Oracle SQL Developer

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Handling PL/<strong>SQL</strong> ErrorsIf you redeclare a global exception in a sub-block, the local declaration prevails. Thesub-block cannot reference the global exception, unless the exception is declared in alabeled block and you qualify its name with the block label:block_label.exception_nameExample 7–30 illustrates the scope rules:Example 7–30Scope of PL/<strong>SQL</strong> ExceptionsDECLAREpast_due EXCEPTION;acct_num NUMBER;BEGINDECLARE ---------- sub-block beginspast_due EXCEPTION; -- this declaration prevailsacct_num NUMBER;due_date DATE := SYSDATE - 1; -- set on purpose to raise exceptiontodays_date DATE := SYSDATE;BEGINIF due_date < todays_date THENRAISE past_due; -- this is not handledEND IF;END; ------------- sub-block endsEXCEPTIONWHEN past_due THEN -- does not handle raised exceptionDBMS_OUTPUT.PUT_LINE('Handling PAST_DUE exception.');WHEN OTHERS THENDBMS_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 ofpast_due in the sub-block prevails. Though they share the same name, the twopast_due exceptions are different, just as the two acct_num variables share the samename but are different variables. Thus, the RAISE statement and the WHEN clause referto different exceptions. To have the enclosing block handle the raised exception, youmust remove its declaration from the sub-block or define an OTHERS handler.7.4.5 Continuing After an Exception is RaisedBy default, you put an exception handler at the end of a subprogram to handleexceptions that are raised anywhere inside the subprogram. To continue executingfrom the spot where an exception happens, enclose the code that might raise anexception inside another BEGIN-END block with its own exception handler. Forexample, you might put separate BEGIN-END blocks around groups of <strong>SQL</strong> statementsthat might raise NO_DATA_FOUND, or around arithmetic operations that might raiseDIVIDE_BY_ZERO. By putting a BEGIN-END block with an exception handler inside aloop, you can continue executing the loop even if some loop iterations raiseexceptions.You can still handle an exception for a statement, then continue with the nextstatement. Place the statement in its own sub-block with its own exception handlers. Ifan error occurs in the sub-block, a local handler can catch the exception. When thesub-block ends, the enclosing block continues to execute at the point where thesub-block ends, as shown in Example 7–31.7-24 <strong>SQL</strong> <strong>Developer</strong> Online Help

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!