Continuing Execution After Handling Exceptions
After an exception handler runs, control transfers to the next statement of the enclosing block (or to the invoker or host environment if there is no enclosing block). The exception handler cannot transfer control back to its own block.
For example, in Example 12-24, after the SELECT
INTO
statement raises ZERO_DIVIDE
and the exception handler handles it, execution cannot continue from the INSERT
statement that follows the SELECT
INTO
statement.
If you want execution to resume with the INSERT
statement that follows the SELECT
INTO
statement, then put the SELECT
INTO
statement in an inner block with its own ZERO_DIVIDE
exception handler, as in Example 12-25.
See Also:
Example 13-13, where a bulk SQL operation continues despite exceptions
Example 12-24 Exception Handler Runs and Execution Ends
DROP TABLE employees_temp; CREATE TABLE employees_temp AS SELECT employee_id, salary, commission_pct FROM employees; DECLARE sal_calc NUMBER(8,2); BEGIN INSERT INTO employees_temp (employee_id, salary, commission_pct) VALUES (301, 2500, 0); SELECT (salary / commission_pct) INTO sal_calc FROM employees_temp WHERE employee_id = 301; INSERT INTO employees_temp VALUES (302, sal_calc/100, .1); DBMS_OUTPUT.PUT_LINE('Row inserted.'); EXCEPTION WHEN ZERO_DIVIDE THEN DBMS_OUTPUT.PUT_LINE('Division by zero.'); END; /
Result:
Division by zero.
Example 12-25 Exception Handler Runs and Execution Continues
DECLARE sal_calc NUMBER(8,2); BEGIN INSERT INTO employees_temp (employee_id, salary, commission_pct) VALUES (301, 2500, 0); BEGIN SELECT (salary / commission_pct) INTO sal_calc FROM employees_temp WHERE employee_id = 301; EXCEPTION WHEN ZERO_DIVIDE THEN DBMS_OUTPUT.PUT_LINE('Substituting 2500 for undefined number.'); sal_calc := 2500; END; INSERT INTO employees_temp VALUES (302, sal_calc/100, .1); DBMS_OUTPUT.PUT_LINE('Enclosing block: Row inserted.'); EXCEPTION WHEN ZERO_DIVIDE THEN DBMS_OUTPUT.PUT_LINE('Enclosing block: Division by zero.'); END; /
Result:
Substituting 2500 for undefined number. Enclosing block: Row inserted.