Oracle PL/SQL - User-Defined Exceptions

Introduction

To declare the exception in the declaration section of the program. The syntax is

<exception_name> exception;

When you raise the exception, you do it by using the RAISE command. The syntax is:

raise <exception_name>;

Handle your exception just as if it were a named predefined exception. The syntax is:

when <exception_name> then

A User-Defined Exception

function f_ValidateSalary(i_empNo_nr NUMBER, i_new_Sal_nr NUMBER)
  return VARCHAR2
is
   v_current_Sal_nr NUMBER;
   e_increaseTooLarge exception; 
begin
    select sal into v_current_Sal_nr
    from emp
    where empNo=i_empNo_nr;
    if (i_newSal_nr/v_current_Sal_nr) * 100 > 300
    then
       raise e_increaseTooLarge; 
    end if;
    return 'Y';                
exception
  when e_increaseTooLarge then 
     insert into t_LogError ...--your table name here--
     return  'N ';             
end;
/

Related Topic