Scope of control of an ON EXCEPTION statement

The scope of the ON EXCEPTION statement extends from the statement that immediately follows the ON EXCEPTION statement, and ends at the end of the statement block in which the ON EXCEPTION statement is issued. If the SPL routine includes no explicit statement blocks, the scope is all subsequent statements in the routine.

For the exceptions specified in the IN clause (or for all exceptions, if no IN clause is specified), the scope of the ON EXCEPTION statement includes all statements that follow the ON EXCEPTION statement within the same statement block. If other statement blocks are nested within that block, the scope also includes all statements in the nested statement blocks that follow the ON EXCEPTION statement, and any statements in statement blocks that are nested within those nested blocks.

The following pseudocode shows where the exception is valid within the routine. That is, if error 201 occurs in any of the indicated blocks, the action labeled a201 occurs.
Figure 1: ON EXCEPTION statement scope of control.
CREATE PROCEDURE scope()
  DEFINE i INT;
       . . .
  BEGIN          -- begin statement block A
       . . .
    ON EXCEPTION IN (201)
    -- do action a201
    END EXCEPTION
    BEGIN         -- nested statement block aa
      -- do action, a201 valid here
    END
    BEGIN         -- nested statement block bb
      -- do action, a201 valid here
    END  
    WHILE i < 10
      -- do something, a201 is valid here
    END WHILE

  END    -- end of statement block A
  BEGIN           -- begin statement block B
    -- do something
    -- a201 is NOT valid here
  END     
END PROCEDURE;