1. Exception An exception is a runtime error or warning condition, which can be predefined or user-defined. Predefined exceptions are raised implicitly (automatically) by the runtime system. User-defined exceptions must be raised explicitly by RAISE statements. To handle raised exceptions, you write separate routines called exception handlers.
2. Types of exception There are two types of exception Predefined exception User defined exception
3. Predefined exception Predefined exceptions are the ones which are already defined by Oracle team that correspond to the most common oracle errors (like division by zero, out of memory, etc.). We can handle them directly within our program without declaring them, and we have thousands of such type of exceptions. In this article, I cover the most important ones.
4.
5. syntax Begin Sequence of statements; Exception When exception name1 then sequence of statements; When exception name2 then sequence of statements; End;
6.
7. Data are not found set serveroutput on declare y wel.sub1%type; begin select sub1 into y from wel where name='mathu'; exception when no_data_found then dbms_output.put_line('datas are not found'); end; Output: PL/SQL procedure successfully completed.
8. set serveroutput on declare y wel.sub1%type; begin select sub1 into y from wel where name='muthu'; exception when no_data_found then dbms_output.put_line('datas are not found'); end; Output: datas are not found PL/SQL procedure successfully completed.
9. Too_many_rows set serveroutput on declare y wel.sub1%type; begin select sub1 into y from wel where sub2=99; exception when too_many_rows then dbms_output.put_line('Two many rows'); end; Output: Two many rows PL/SQL procedure successfully completed.
10. User Defined Exception User-Defined Exceptions must be declared and raised explicitly by the user (by issuing RAISE statements). These exceptions are created, used, raised and implemented by user himself. Oracle will not know about any of those exceptions (till it finds the declarations of those exceptions within the PL/SQL block). There exists EXCEPTION section within the PL/SQL block to handle any sort of exceptions and the section is optional. Let us start by looking into some of the most commonly used predefined exceptions.
11. Raise statement raise_application_error( error_number, message[, {TRUE | FALSE}]); error_number it is a negative integer in the range -20000 .. -20999 message is a character string up to 2048 bytes long. If the optional third parameter is TRUE, the error is placed on the stack of previous errors. If the parameter is FALSE (the default), the error replaces all previous errors. RAISE_APPLICATION_ERROR is part of package DBMS_STANDARD, and as with package STANDARD, you do not need to qualify references to it.
12. set serveroutput on prompt 1.addition prompt 2.subtraction prompt 3.multplication prompt 4.division accept n number prompt 'Enter your choice:' declare d number := &n; a number := &a; b number := &b; c number(5); add_excep exception; sub_excep exception; mul_excep exception; div_excep exception;
13. begin if d=1 then if(a>0 and b>0)then dbms_output.put_line('ADDTION:’ ||c); else raisadd_excep; end if; end if;
14. if d=2 then if(a>0 and b>0) then c:=a-b; dbms_output.put_line('subtraction:'||c); else raise sub_excep; end if; end if;
15. if d=3 then if(a>0 and b>0) then c:=a*b; dbms_output.put_line('multiplication:'||c); else raise mul_excep; end if; end if;
16. if d=4 then if(a>0 and b>0)then c:=a/b; dbms_output.put_line('division:'||c); else raise div_excep; end if; end if;
17. exception when add_excep then dbms_output.put_line('arithmetic exception'); when sub_excep then dbms_output.put_line("enter passitive values'); when mul_excep then dbms_output.put_line('enter valid input'); when div_excep then dbms_output.put_line('divided by zero exception'); end;
18. Output 1.addition 2.subtraction 3.multplication 4.division old 2: d number := &n; new 2: d number := 3; old 3: a number := &a; new 3: a number := 10; old 4: b number := &b; new 4: b number := 23; multiplication: 230PL/SQL procedure successfully completed.