Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors."— Presentation transcript:

1 Handling Exceptions

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

3

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

5

6

7

8 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

9

10

11 Predefined Oracle server Errors Exception NameDescription CASE_NOT_FOUNDNone of the choices in the WHEN clauses of a CASE statement are selected, and there is no ELSE clause. NO_DATA_FOUNDSingle row SELECT returned no data. TOO_MANY_ROWSSingle-row SELECT returned more than one row. ZERO_DIVIDEAttempted to divide by zero

12 Tutorial Write a PL/SQL block to select the last name of the employee with a given salary = 2500 a. 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.” 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.” c. Handle any other exception with an appropriate exception handler and insert into the messages table the message “Some other error occurred.”

13 SET SERVEROUTPUT ON DECLARE Lname employees.Last_name%TYPE; Sal employees.salary%TYPE := 2500 ; BEGEN SELECT 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 THEN DBMS_OUTPUT.PUT_LINE( ‘No employee with a salary of ’ || Sal); WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE( ‘More than one employee with a salary of ’ || Sal); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE( ‘Some other error occurred’); END;


Download ppt "Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors."

Similar presentations


Ads by Google