Top Banner
Exception Handling in PL/SQL
22

Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Dec 21, 2015

Download

Documents

Janis Boone
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Exception Handling

in PL/SQL

Page 2: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

POINTS TO DISCUSS

• What is Exception Handling

• Structure of Exception Handling Section

• Types of Exceptions

Page 3: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

What is Exception Handling

•Error condition in PL/SQL is called exception.

•A part of Executable section•Between Begin… and end;• Using Exception Handling we can test

the code and avoid it from exiting abruptly.

•When an exception occurs a message which explains its cause is received.

Page 4: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Exception consists of 3 parts

•Type of Exception

•An Error Code

•A message By handling exceptions we ensure a PL/SQL block does not exit abruptly.

Page 5: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Syntax for exception DECLARE

Exception Declaration BEGIN

Executable statements…EXCEPTION

WHEN ex_name1 THEN -Error handling statements WHEN ex_name2 THEN -Error handling statements WHEN Others THEN -Error handling statements

END;

When an exception is raised, Oracle searches for an appropriate exception handler in the exception section.

Only one exception can be raised in a Block and the control does not return to the Execution Section after the error is handled.

Page 6: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Types of Exceptions

•Pre defined Exceptions

•Unnamed System Exceptions

•User-defined Exceptions

Page 7: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Named System Exceptions (Predefined)

• Automatically raised by Oracle when a program violates a RDBMS rule

• System exceptions that are raised frequently are pre-defined and given a name in Oracle

• Not Declared explicitly• Raised implicitly when a predefined Oracle

error occurs• Caught by referencing the standard name

within an exception-handling routine

Page 8: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Some Named (Predefined) ExceptionsException Name Reason Error Number

CURSOR_ALREADY_OPEN When you open a cursor that is already open.

ORA-06511

INVALID_CURSOR When you perform an invalid operation on a cursor like closing a cursor, fetch data from a cursor that is not opened.

ORA-01001

NO_DATA_FOUND When a SELECT...INTO clause does not return any row from a table.

ORA-01403

TOO_MANY_ROWS When you SELECT or fetch more than one row into a record or variable.

ORA-01422

ZERO_DIVIDE When you attempt to divide a number by zero.

ORA-01476

Page 9: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Predefined Exception Exampledeclarev_empno emp.empno%type;v_ename emp.ename%type;v_sal emp.sal%type;a number;begina:=&a;select empno,ename,sal into v_empno,v_ename,v_sal from emp where empno=&v_empno;v_sal:=v_sal+v_sal/a;dbms_output.put_line('Employee number is '||v_empno || ' and name is '||v_ename);dbms_output.put_line('New salary is '||v_sal);exceptionwhen no_data_found thendbms_output.put_line('NO SUCH RECORD!!!');when zero_divide thendbms_output.put_line('Salary cannot be divided by zero');when others thendbms_output.put_line('Some error.GOD knows what???');end;/

Page 10: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Unnamed System Exceptions• System exception for which oracle does not

provide a name is known as unnamed system exception.

• These exceptions do not occur frequently.• These Exceptions have a code and an associated

message. • There are two ways to handle unnamed system

exceptions: 1. By using the WHEN OTHERS exception handler, or 2. By associating the exception code to a name and using it as a named exception.

Page 11: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Unnamed exceptions

•We can assign a name to unnamed system exceptions using Pragma EXCEPTION_INIT.

•EXCEPTION_INIT will associate a predefined Oracle error number to a programmer_defined exception name.

Page 12: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Syntax for unnamed system exception using EXCEPTION_INIT

DECLARE exception_name EXCEPTION; PRAGMA EXCEPTION_INIT (exception_name, Err_code); BEGIN Execution sectionEXCEPTION WHEN exception_name THEN handle the exceptionEND;

Page 13: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

ExampleDECLAREdup_pr_key exception;pragma exception_init(dup_pr_key,-1);BEGINinsert into emp(empno,ename) values(1111,'ABCD');dbms_output.put_line('One record successfully inserted');insert into emp(empno,ename) values(1111,'EFGH');dbms_output.put_line('One more record successfully inserted');EXCEPTIONwhen dup_pr_key thendbms_output.put_line('How come more employees with the same employee number !!!');END;/

Page 14: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

User-defined Exceptions

• They should be explicitly declared in the declaration section.

• They should be explicitly raised in the Execution Section.

• They should be handled by referencing the user-defined exception name in the exception section.

Page 15: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

User defined Exception-Exampledeclarev_empno emp.empno%type;v_ename emp.ename%type;v_sal emp.sal%type;a number;lo_sal exception;begina:=&a;select empno,ename,sal into v_empno,v_ename,v_sal from emp where empno=&v_empno;v_sal:=v_sal+v_sal/a;if v_Sal<2000 thenraise lo_sal;end if;dbms_output.put_line('Employee number is '||v_empno || ' and name is '||v_ename);dbms_output.put_line('New salary is '||v_sal);exceptionwhen lo_sal then

dbms_output.put_line('In these times of Inflation INCREASE THE SALARY !!!');when others then

dbms_output.put_line('Some error.GOD knows what???');end;

Page 16: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

RAISE_APPLICATION_ERROR ( )

• a built-in procedure in oracle used to display the user-defined error messages along with the error number

• range of error number between -20000 and -20999.

• Whenever a message is displayed using RAISE_APPLICATION_ERROR, all previous transactions which are not committed within the PL/SQL Block are rolled back automatically (i.e. change due to INSERT, UPDATE, or DELETE statements).

• raises an exception but does not handle it.

Page 17: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

WHY RAISE_APPLICATION_ERROR ?

•to create a unique id for an user-defined exception.

• to make the user-defined exception look like an Oracle error.

Page 18: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Syntax

RAISE_APPLICATION_ERROR (error_number, error_message);

•The Error number must be between -20000 and -20999

•The Error message is the message you want to display when the error occurs.

Page 19: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

Steps for RAISE_APPLICATION_ERROR procedure:

•Declare a user-defined exception in the declaration section.

•Raise the user-defined exception based on a specific business rule in the execution section.

•Finally, catch the exception and link the exception to a user-defined error number in RAISE_APPLICATION_ERROR.

Page 20: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

RAISE_APPLICATION_ERROR -ExampleDECLAREv_empno emp.empno%type;v_ename emp.ename%type;v_sal emp.sal%type;a number;lo_sal exception;BEGINa:=&a;select empno,ename,sal into v_empno,v_ename,v_sal from emp

where empno=&v_empno;v_sal:=v_sal+v_sal/a;if v_Sal<2000 thenraise lo_sal;end if;dbms_output.put_line('Employee number is '||v_empno || ' and name is '||v_ename);dbms_output.put_line('New salary is '||v_sal);

Page 21: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

RAISE_APPLICATION_ERROR –Example(contd.)EXCEPTIONwhen lo_sal thenRAISE_APPLICATION_ERROR(-20001,'In these times of Inflation INCREASE THE SALARY !!!');when others thendbms_output.put_line('Some error.GOD knows what???');END;

Page 22: Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.

THANKS