Exception Propagation in Nested Blocks - Exercise

Contact Us or call 1-877-932-8228
Exception Propagation in Nested Blocks - Exercise

Exception Propagation in Nested Blocks

Duration: 10 to 15 minutes.
  1. Look at the code in the exercise.
  2. Answer the following questions:
    • Will the code compile as written?
    • Is the variable my_exception visible at the time it is raised? Why or why not?
    • Will my_exception be handled in any/all of the exception handling sections?
    • What do you expect the output of the program to be?
    • See the solution below or execute the code for yourself.

Code Sample:

Nested-Blocks/Exercises/nested_blocks_and_exceptions.sql
declare
  my_exception exception;
begin

   dbms_output.put_line('Begin processing in outer block.');

  begin
    dbms_output.put_line('Begin processing in inner block.');
    raise my_exception;
    -- Will never get here.
    dbms_output.put_line('Completed inner block.');
  exception
    when my_exception then
      dbms_output.put_line('Caught exception in the inner block: '
                           || substr(sqlerrm, 1, 100));
  end;

  dbms_output.put_line('Completed outer block.');  

exception
  when others then
    dbms_output.put_line('Caught exception in the outer block.');
end;

Code Explanation

Solution:

Nested-Blocks/Solutions/nested_blocks_and_exceptions.txt
Begin processing in outer block.
Begin processing in inner block.
Caught exception in the inner block: User-Defined Exception
Completed outer block.

Code Explanation

Next