Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions...

13
Handling Exceptions

Transcript of Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions...

Page 1: Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.

Handling Exceptions

Page 2: Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.

Objectives

• What is exception• Types of exceptions• How to handle exceptions• Trapping pre defined oracle errors

Page 3: Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Page 4: Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.

Example of an exception

• Consider the example shown in the slide. There are no syntax errors in the code, which means you must be able to successfully execute the anonymous block. The select statement in the block retrieves the last_name of John.

• The code does not work as expected. You expected the SELECT statement to retrieve only one row; however, it retrieves multiple rows. Such errors that occur at run time are called exceptions.

• When an exception occurs, the PL/SQL block is terminated. You can handle such exceptions in your PL/SQL block.

Page 5: Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Page 6: Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Page 7: Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Page 8: Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.

Trapping Exception

exception Is the standard name of a predefined exception or the name of a user-defined exception declared within the declarative section

statement Is one or more PL/SQL or SQL statements

OTHERS Is an optional exception-handling clause that traps any exceptions that have not been explicitly handled

Page 9: Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Page 10: Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Page 11: Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.

Predefined Oracle server ErrorsException Name Description

CASE_NOT_FOUND None of the choices in the WHEN clauses of a CASE statement are selected, and there is no ELSE clause.

NO_DATA_FOUND Single row SELECT returned no data.

TOO_MANY_ROWS Single-row SELECT returned more than one row.

ZERO_DIVIDE Attempted to divide by zero

Page 12: Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.

Tutorial• Write a PL/SQL block to select the last name of the employee with a

given salary = 2500a. If the salary entered does not return any rows, handle the exception

with an appropriate exception handler and insert into the messages table the message “No employee with a salary of <salary>.”

b. If the salary entered returns more than one row, handle the exception with an appropriate exception handler and insert into the messages table the message “More than one employee with a salary of <salary>.”

c. Handle any other exception with an appropriate exception handler and insert into the messages table the message “Some other error occurred.”

Page 13: Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.

SET SERVEROUTPUT ONDECLARE

Lname employees.Last_name%TYPE;Sal employees.salary%TYPE := 2500 ;

BEGENSELECT last_name INTO lname FROM employees WHERE salary = Sal ;DBMS_OUTPUT.PUT_LINE( ‘the last name of the employee with the salary 2500 is : ’ ||

Lname );EXCEPTION

WHEN NO_DATA_FOUND THENDBMS_OUTPUT.PUT_LINE( ‘No employee with a salary of ’ || Sal); WHEN TOO_MANY_ROWS THENDBMS_OUTPUT.PUT_LINE( ‘More than one employee with a salary of ’ || sal); WHEN OTHERS THENDBMS_OUTPUT.PUT_LINE( ‘Some other error occurred’);

END;