Exception Error handling: action to be peformed when error occours. Pre-defined exception ..................... Pre-defined exception: Zero_divide example Without Excepion ... begin dbms_output.put_line('print 1'); dbms_output.put_line(3/0); dbms_output.put_line('print 3'); end; / Error: ORA-01476: divisor is equal to zero ... With Excepion ... begin dbms_output.put_line('print 1'); dbms_output.put_line(3/0); dbms_output.put_line('print 3'); exception when zero_divide then dbms_output.put_line('print 2'); end; / Output: print 1 print 2 Statement processed. Pre-defined exception: Dup_val_on_index In table t1(a) a is primary key. primary key does not accept duplicate values and null values. when we insert the duplicate values ... example Without Excepion ... begin insert into t1 values(1); insert into t1 values(2); insert into t1 values(1); end; / Error: ORA-00001: unique constraint (HR.Q1) violated ... With Excepion ... begin insert into t1 values(1); insert into t1 values(2); insert into t1 values(1); exception when dup_val_on_index then dbms_output.put_line('print 3'); end; / output: print 3 Statement processed. ... Pre-defined exception: Too_many_rows Example Without Excepion ... declare i employees.first_name%type; begin select first_name into i from employees where rownum<=5; dbms_output.put_line(i); end; / Error: ORA-01422: exact fetch returns more than requested number of rows ... With Excepion ... declare i employees.first_name%type; begin select first_name into i from employees where rownum<=5; dbms_output.put_line(i); exception when too_many_rows then dbms_output.put_line('print 5'); end; / Output: print 5 Statement processed. ... Pre-defined exception: No_data_found Example Without Excepion ... declare i employees.first_name%type; begin select first_name into i from employees where rownum<=0; dbms_output.put_line(i); end; / Error: ORA-01403: no data found ... With Excepion ... declare i employees.first_name%type; begin select first_name into i from employees where rownum<=0; dbms_output.put_line(i); exception when no_data_found then dbms_output.put_line('print 6'); end; / Output: print 6 Statement processed. ... Pre-defined exception: Others Others handler must be in the last among the exception handlers of a block. Example: ... declare i employees.first_name%type; begin select first_name into i from employees where rownum<=0; dbms_output.put_line(i); exception when too_many_rows then dbms_output.put_line('print 5'); when others then dbms_output.put_line('print 6'); end; / Output: print 6 Statement processed. ... SqlCode ........ Example ... begin dbms_output.put_line('print 1'); dbms_output.put_line(3/0); exception when others then dbms_output.put_line(sqlcode); end; / ... OutPut; print 1 -1476..............sqlcode Statement processed. ... Sqlerrm- Error Name ........ Example: ... begin dbms_output.put_line('print 1'); dbms_output.put_line(3/0); exception when others then dbms_output.put_line(sqlerrm); end; / Output: ... print 1 ORA-01476: divisor is equal to zero.............Error Name Statement processed. ... Dbms_utility.format_error_stack -Error Name ............................... Example: ... begin dbms_output.put_line('print 1'); dbms_output.put_line(3/0); exception when others then dbms_output.put_line(dbms_utility.format_error_stack); end; / Output: ... print 1 ORA-01476: divisor is equal to zero.............Error Name Statement processed. Dbms_utility.format_error_backtrace - Error line ................................... Example: ... begin dbms_output.put_line('print 1'); dbms_output.put_line(3/0); exception when others then dbms_output.put_line(Dbms_utility.format_error_backtrace); end; / ... Output: ... print 1 ORA-06512: at line 3 Statement processed. User Defined Exception ...................... To throw the Error- Use 'raise' Example: ... declare greens exception; begin dbms_output.put_line('print 1'); raise greens; dbms_output.put_line('print 2'); exception when greens then dbms_output.put_line('print 3'); end; / ... Output: print 1 print 3 Statement processed. ... Non-predefined Exception- pragma exception_init ........................ Example ... declare greens exception; pragma exception_init(greens,-01476); begin dbms_output.put_line('print 1'); dbms_output.put_line(6/0); dbms_output.put_line('print 2'); exception when greens then dbms_output.put_line('print 3'); end; / Output: print 1 print 3 Statement processed. ... User Defined Error .................. User can create the error using 'raise_application_error' Error code range can be -20000 to -20999 Example: ... begin dbms_output.put_line('print 1'); raise_application_error(-20000,'User Error'); dbms_output.put_line('print 2'); end; / Output: ... ORA-20000: User Error.