Using the IN Clause to Trap Specific Exceptions

An error is trapped if the SQL error code or the ISAM error code matches an exception code in the list of error numbers. The search through the list of errors begins from the left and stops with the first match. You can use a combination of an ON EXCEPTION statement without an IN clause and one or more ON EXCEPTION statements with an IN clause. When an error occurs, the database server searches for the last declaration of the ON EXCEPTION statement that traps the particular error code.
CREATE PROCEDURE ex_test()
   DEFINE error_num INT;
   ...
   ON EXCEPTION SET error_num
   -- action C
   END EXCEPTION
   ON EXCEPTION IN (-300)
   -- action B
   END EXCEPTION
   ON EXCEPTION IN (-210, -211, -212) SET error_num
   -- action A
   END EXCEPTION
A summary of the sequence of statements in the previous example would be:
  1. Test for an error.
  2. If error -210, -211, or -212 occurs, take action A.
  3. If error -300 occurs, take action B.
  4. If any other error occurs, take action C.