Catch custom exception : Handle Exception « PL SQL Programming « Oracle PL/SQL Tutorial






SQL> set serveroutput on
SQL> DECLARE
  2       quantity1 NUMBER := -2;
  3       quantity2 NUMBER := 3;
  4       total NUMBER := 0;
  5       quantity_must_positive EXCEPTION;
  6       FUNCTION find_cost (quant NUMBER) RETURN NUMBER IS
  7       BEGIN
  8            IF (quant > 0)
  9            THEN
 10                 RETURN(quant * 20);
 11            ELSE
 12                 RAISE quantity_must_positive;
 13            END IF;
 14       END find_cost;
 15  BEGIN
 16       total := find_cost (quantity2);
 17       total := total + find_cost(quantity1);
 18  EXCEPTION
 19       WHEN quantity_must_positive
 20       THEN
 21            dbms_output.put_line('Total until now: ' || total);
 22            dbms_output.put_line('Tried to use negative quantity ');
 23  END;
 24  /
Total until now: 60
Tried to use negative quantity

PL/SQL procedure successfully completed.

SQL>
SQL>








24.15.Handle Exception
24.15.1.Code with No Exception Handler
24.15.2.Code with Conditional Control to Avoid an Exception
24.15.3.Code with Explicit Handler for Predefined Exception
24.15.4.Handling an Unnamed Exception
24.15.5.Handling a custom exception
24.15.6.An example showing continuing program execution after handling exception
24.15.7.The OTHERS Exception Handler
24.15.8.Assigning a Name to Predefined Exception Code
24.15.9.Using SQLCODE for error code and SQLERRM for error message
24.15.10.Catch custom exception
24.15.11.Handling exceptions without halting the program
24.15.12.Select into statement with exception catch statement
24.15.13.Check OTHERS exception
24.15.14.Error message code and text
24.15.15.Using PRAGMA EXCEPTION_INIT
24.15.16.NO data found
24.15.17.TOO_MANY_ROWS Exception
24.15.18.Use a nested block to catch exceptions from singleton SELECT.