exception
TRANSCRIPT
ExceptionAn 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.
Types of exceptionThere are two types of exception
Predefined exceptionUser defined exception
Predefined exceptionPredefined 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.
Predefined exception RAISED IFToo_many rows Select into statement return
more than one row
No_data_found Select statement return no rows
Cursor_already_open Try to open a cursor already opened
Zero_divide Try to divide a number by zero
Dup_val_on_index Insert duplicates values in a column which is defined as unique.
Storage_error PL/SQL runs out of memory
Invalid_cursor Violate cursor operation
Login_denied Try to enter oracle using invalid u/p
Invalid_number Conversion of a character string to a number fails
Program_error Pl/sql has an internal problem
syntaxBeginSequence of statements;ExceptionWhen exception name1 then sequence
of statements;When exception name2 then sequence
of statements;End;
REGNO NAME SUB1 SUB2 SUB3
1001 mathu 90 99 99
111 jeya 99 99 99
5676 mitha 78 67 67
2345 raj 89 99 90
Data are not foundset serveroutput ondeclarey wel.sub1%type;beginselect sub1 into y from wel where name='mathu';exceptionwhen no_data_found thendbms_output.put_line('datas are not found');end;Output:
PL/SQL procedure successfully completed.
set serveroutput ondeclarey wel.sub1%type;beginselect sub1 into y from wel where name='muthu';exceptionwhen no_data_found thendbms_output.put_line('datas are not found');end;Output:datas are not found
PL/SQL procedure successfully completed.
Too_many_rowsset serveroutput ondeclarey wel.sub1%type;beginselect sub1 into y from wel where sub2=99;exceptionwhen too_many_rows thendbms_output.put_line('Two many rows');end;Output:Two many rows
PL/SQL procedure successfully completed.
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.
Raise statementraise_application_error(
error_number, message[, {TRUE | FALSE}]); error_number it is a negative integer in the range -
20000 .. -20999message 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.
set serveroutput onprompt 1.additionprompt 2.subtractionprompt 3.multplicationprompt 4.divisionaccept 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;
beginif d=1 thenif(a>0 and b>0)thendbms_output.put_line('ADDTION:’ ||c);
elserais add_excep;end if;end if;
if d=2 thenif(a>0 and b>0) thenc:=a-b;dbms_output.put_line('subtraction:'||c);
elseraise sub_excep;end if;end if;
if d=3 then if(a>0 and b>0) thenc:=a*b;dbms_output.put_line('multiplication:'||c);
elseraise mul_excep;end if;end if;
if d=4 thenif(a>0 and b>0)thenc:=a/b;dbms_output.put_line('division:'||c);
elseraise div_excep;end if;end if;
exception
when add_excep thendbms_output.put_line('arithmetic exception'); when sub_excep thendbms_output.put_line("enter passitive values'); when mul_excep thendbms_output.put_line('enter valid input'); when div_excep then dbms_output.put_line('divided by zero exception');end;
Output1.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.
Department of CE/ITMSPVL Polytechnic collegePavoorchatram