Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle nested blocks and exception handling

DECLARE
    string_of_5_chars VARCHAR2(5);
BEGIN
    BEGIN
        string_of_5_chars := 'Steven';
    EXCEPTION
        WHEN value_error THEN
          RAISE no_data_found;
        WHEN no_data_found THEN
          dbms_output.Put_line ('Inner block');
    END;
EXCEPTION
    WHEN no_data_found THEN
      dbms_output.Put_line ('Outer block');
END; 

Answer says that the output will be 'Outer block' , Can somebody explain why the inner block would not be executed ? What is the precedence of exceptions in oracle

like image 317
redsoxlost Avatar asked Nov 26 '13 13:11

redsoxlost


People also ask

What should I look for in a nested exception block?

Read here for more information about nested exception blocks. You should consider the exception block's WHEN clauses as being similar to a regular CASE statement. The first WHEN that matches the condition executes, and the following WHEN clauses in that exception handler are skipped.

How to use try block in Java to handle exceptions?

In Java, we can use a try block within a try block. Each time a try statement is entered, the context of that exception is pushed on to a stack. Given below is an example of a nested try. In this example, inner try block (or try-block2) is used to handle ArithmeticException, i.e., division by zero.

What happens to the second when clause in a nested exception?

The first WHEN that matches the condition executes, and the following WHEN clauses in that exception handler are skipped. Therefore the second WHEN clause in the inner exception block is not in the code execution path at all, and the outer exception block catches the no_data_found error raised by the first WHEN clause of the nested exception.

What happens if the catch block does not handle the exception?

If none of the catch blocks handles the exception then the Java run-time system will handle the exception and a system generated message would be shown for the exception. Writing code in comment?


2 Answers

DECLARE
string_of_5_chars VARCHAR2(5);
BEGIN
BEGIN
    string_of_5_chars := 'Steven';  -- Varchar has a size of 5 defined above. So it will throw a value_error(due to size constraints) exception.
EXCEPTION
    WHEN value_error THEN    -- This exception block will handle the error thrown above.
      RAISE no_data_found;   -- It raises a no_data _found exception which by rule has to be handled in the outer exception block. So it goes to the outer exception block.
    WHEN no_data_found THEN
      dbms_output.Put_line ('Inner block');
END;
EXCEPTION
WHEN no_data_found THEN
  dbms_output.Put_line ('Outer block'); -- Exception is handled here which causes it to print 'Outer Block'
END;

Read here for more information about nested exception blocks.

like image 114
Adarsh Avatar answered Oct 13 '22 01:10

Adarsh


You should consider the exception block's WHEN clauses as being similar to a regular CASE statement. The first WHEN that matches the condition executes, and the following WHEN clauses in that exception handler are skipped.

Therefore the second WHEN clause in the inner exception block is not in the code execution path at all, and the outer exception block catches the no_data_found error raised by the first WHEN clause of the nested exception.

Exception propagation in this scenario is explained here: http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/errors.htm#LNPLS00706

like image 24
David Aldridge Avatar answered Oct 12 '22 23:10

David Aldridge