Raise Exception in a function : Raise 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.16.Raise Exception
24.16.1.User-Defined Errors
24.16.2.Raise Exception in a function
24.16.3.Raising an Exception Local PL/SQL Block
24.16.4.Avoiding exceptions raised in declaration part and exception handler
24.16.5.Raising an Exception in the Declaration Section
24.16.6.Exceptions Raised in the Exception Handler
24.16.7.Raising NO_DATA_FOUND
24.16.8.Raising a custom exception
24.16.9.Using WHEN OTHERS clause
24.16.10.Using SQLCODE and SQLERRM
24.16.11.Propogating a Server-side Customized Error Number and Error Message to client program using PRAGMA EXCEPTION_INIT