Skip to content

Exception Propagation ๐Ÿซง

Mentor's Note: Imagine you are in a tall office building. If a fire starts on the 1st floor and there is no firefighter there, the smoke travels up to the 2nd floor. In PL/SQL, if an inner block doesn't handle an error, it "bubbles up" to the outer block. This is called Propagation. ๐Ÿ’ก


๐ŸŒŸ The Scenario: The Message Relay ๐Ÿ“ž

  • Inner Block: You find an error. You don't know what to do.
  • The Bubble: You pass the error up to your manager (The Outer Block).
  • The Outer Block: If the manager knows what to do, the program continues. If not, the program crashes! โœ…

๐Ÿ’ป 1. The Propagation Flow

If an error occurs in a nested block: 1. Oracle looks for a handler in the Current Block. 2. If not found, it stops the inner block and looks in the Outer Block. 3. It keeps going until a handler is found or it hits the App level.

DECLARE
   -- ๐Ÿ“ฆ Outer Block
BEGIN
   DECLARE
      -- ๐Ÿ“ฆ Inner Block
   BEGIN
      RAISE_APPLICATION_ERROR(-20001, 'Inner Error');
      -- โŒ Inner block has no handler!
   END;
EXCEPTION
   WHEN OTHERS THEN
      -- โœ… Outer block catches the "Bubbled Up" error
      DBMS_OUTPUT.PUT_LINE('Caught in Outer: ' || SQLERRM);
END;

๐ŸŽจ Visual Logic: Bubbling Up

graph TD
    A[Inner Block Error! ๐Ÿ’ฅ] --> B{Handled Locally?}
    B -- No --> C[Stop Inner Block ๐Ÿ›‘]
    C --> D[Outer Block Exception Handler ๐Ÿช‚]
    D -- "Not Handled?" --> E[Crash App ๐Ÿงจ]
    D -- "Handled!" --> F[Continue Program โœ…]

๐Ÿ—๏ธ Architect's Note: Re-Raising ๐Ÿ›ก๏ธ

Sometimes you want to log an error in the inner block but still let the outer block know it happened. You use the RAISE; command with no name.

EXCEPTION
   WHEN OTHERS THEN
      Log_Error_To_File(SQLERRM);
      RAISE; -- ๐Ÿš€ Send the error to the next level

๐Ÿ“ˆ Learning Path