Skip to content

Exception Propagation 🫧¢

Prerequisites: Exception Basics

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ΒΆ