Scope of an Exception
BEGIN
DECLARE ---------- sub-block begins
past_due EXCEPTION;
due_date DATE := Trunc(SYSDATE)
- 1;
todays_date DATE := Trunc(SYSDATE);
BEGIN
IF due_date < todays_date THEN
RAISE past_due;
END IF;
END; ------------- sub-block ends
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END;
/
OUTPUT:-
PL/SQL procedure successfully completed.
---------------------------------------------------
Because the block that declares the exception past_due has no handler for it, the exception propagates to the enclosing block. But the enclosing block cannot reference he name PAST_DUE, because the scope where it was declared no longer exists. Once the exception name is lost, only an OTHERS handler can catch the exception. If there is no handler for a user-defined exception, the calling application gets this error:
ORA-06510: PL/SQL: unhandled user-defined exception
BEGIN
DECLARE ---------- sub-block begins
past_due EXCEPTION;
due_date DATE := Trunc(SYSDATE)
- 1;
todays_date DATE := Trunc(SYSDATE);
BEGIN
IF due_date < todays_date THEN
RAISE past_due;
END IF;
END; ------------- sub-block ends
--EXCEPTION
--WHEN OTHERS THEN
--ROLLBACK;
END;
/
BEGIN
*
ERROR at line 1:
ORA-06510: PL/SQL: unhandled user-defined exception
ORA-06512: at line 8
0 comments:
Post a Comment