This figure shows an exception and an associated exception handler. A trigger uses a conditional statement that checks for an error condition and raises an application error. The user-defined error code and message are returned to the error handler.
PROCEDURE fire_emp (empid NUMBER) IS invalid_empid EXCEPTION PRAGMA EXCEPTION_INIT (invalid_empid, -20101); BEGIN DELETE FROM emp WHERE empno = empid; EXCEPTION WHEN invlid_empid THEN INSERT INTO emp_audit VALUES (empid, 'Fired before probation ended'); END;
This program returns the error number 20101 to the invoking environment (the preceding program).
TRIGGER emp_probation BEFORE DELETE ON emp FOR EACH ROW BEGIN IF (sysdate - :old.hiredate) < 30 THEN raise_application_error(20101, 'Employee' || old.ename || ' on probation') END IF; END;