PL/SQL: Exceptions Cheat Sheet

Exception Handling

The default exception will be handled using WHEN others THEN

DECLARE 
   <declarations section> 
BEGIN 
   <executable command(s)> 
EXCEPTION 
   <exception handling goes here > 
   WHEN exception1 THEN  
      exception1-handling-statements  
   WHEN exception2  THEN  
      exception2-handling-statements  
   WHEN exception3 THEN  
      exception3-handling-statements 
   ........ 
   WHEN others THEN 
      exception3-handling-statements 
END;
Raising Exceptions

Below is the simple syntax for raising an exception:

DECLARE 
   exception_name EXCEPTION; 
BEGIN 
   IF condition THEN 
      RAISE exception_name; 
   END IF; 
EXCEPTION 
   WHEN exception_name THEN 
   statement; 
END; 
User-defined Exceptions
DECLARE 
   my-exception EXCEPTION; 

Leave a Reply

Your email address will not be published. Required fields are marked *