exception

19
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.

Upload: work

Post on 18-May-2015

375 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Exception

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.

Page 2: Exception

Types of exceptionThere are two types of exception

Predefined exceptionUser defined exception

Page 3: 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.

Page 4: Exception

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

Page 5: Exception

syntaxBeginSequence of statements;ExceptionWhen exception name1 then sequence

of statements;When exception name2 then sequence

of statements;End;

Page 6: Exception

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

Page 7: Exception

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.

Page 8: Exception

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.

Page 9: Exception

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.

Page 10: Exception

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.

Page 11: Exception

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.

Page 12: Exception

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;

Page 13: Exception

beginif d=1 thenif(a>0 and b>0)thendbms_output.put_line('ADDTION:’ ||c);

elserais add_excep;end if;end if;

Page 14: Exception

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;

Page 15: Exception

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;

Page 16: Exception

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;

Page 17: Exception

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; 

Page 18: Exception

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.

Page 19: Exception

Department of CE/ITMSPVL Polytechnic collegePavoorchatram