plsql_07_01_handling_exceptions.doc

5
Your Name PL/SQL 07-01 Date Submitted 07-01: Handling Exceptions Terminology Directions: Identify the vocabulary word for each definition below: AN EXCEPTION HANDLER 1. Code that defines the recovery actions to be performed when execution-time errors occur. AN EXCEPTION 2. Occurs when an error is discovered during the execution of a program that disrupts the normal operation of the program. Try It / Solve It 1. What happens when Oracle encounters a runtime problem while executing a PL/SQL block? An exception occurs when an error is discovered during the execution of a program that disrupts the normal operation of the program. 2. What do you need to add to your PL/SQL block to address these problems? AN EXCEPTION HANDLER 3. List three advantages of handling exceptions within a PL/SQL block. Protects the user from errors Protects the database from errors Major errors take a lot of system resources Code is more readable For all the following problems, each time you run the code in APEX copy the results into your answer. 4. Run this PL/SQL code and then answer the questions that follow. DECLARE v_jobid employees.job_id%TYPE; BEGIN SELECT job_id INTO v_jobid FROM employees WHERE department_id = 80; END; Page 1 of 5

Transcript of plsql_07_01_handling_exceptions.doc

Page 1: plsql_07_01_handling_exceptions.doc

Your Name PL/SQL 07-01 Date Submitted

07-01: Handling Exceptions

TerminologyDirections: Identify the vocabulary word for each definition below:AN EXCEPTION HANDLER 1. Code that defines the recovery actions to be performed when execution-time errors occur.

AN EXCEPTION 2. Occurs when an error is discovered during the execution of a program that disrupts the normal operation of the program.

Try It / Solve It1. What happens when Oracle encounters a runtime problem while executing a PL/SQL block?

An exception occurs when an error is discovered during the execution of a program that disrupts the normal operation of the program.

2. What do you need to add to your PL/SQL block to address these problems?

AN EXCEPTION HANDLER

3. List three advantages of handling exceptions within a PL/SQL block.

Protects the user from errors Protects the database from errors Major errors take a lot of system resources Code is more readable

For all the following problems, each time you run the code in APEX copy the results into your answer.4. Run this PL/SQL code and then answer the questions that follow.

DECLAREv_jobid employees.job_id%TYPE;

BEGINSELECT job_id INTO v_jobid

FROM employeesWHERE department_id = 80;

END;

A. What happens when you run the block? Why?

ORA-01422: exact fetch returns more than requested number of rows

B. In your own words, explain what can you do to fix this problem.

I could modify the where clause or add an exception to handle the error.

Page 1 of 4

Page 2: plsql_07_01_handling_exceptions.doc

Your Name PL/SQL 07-01 Date Submitted

C. Modify the code to fix the problem. Use a TOO_MANY_ROWS exception handler.

DECLAREv_jobidemployees.job_id%TYPE;BEGINSELECT job_id INTO v_jobidFROM employeesWHERE department_id = 80;EXCEPTIONWHEN TOO_MANY_ROWSTHENDBMS_OUTPUT.PUT_LINE ('Too many rows returned from the query');END;

D. Run your modified code. What happens this time?

Too many rows returned from the query

Statement processed.

5. Run the following PL/SQL block, which tries to insert a new row (with department_id = 50)into the departments table. What happens and why?BEGIN

INSERT INTO departments (department_id, department_name, manager_id, location_id)VALUES (50, ‘A new department’, 100, 1500);

DBMS_OUTPUT.PUT_LINE(‘The new department was inserted’);EXCEPTION

WHEN OTHERS THENDBMS_OUTPUT.PUT_LINE ('An exception has occurred.');

END;

An exception has occurred.

Statement processed.

The INSERT statement failed because department_id 50 already exists and department_id is a unique column. The rest of the code was then skipped and the exception string was outputted.

6. Enter the following PL/SQL block, which tries to SELECT all the employees in a specificdepartment. Run it three times, using department_ids 10, 20 and 30 and for each value explain what happens and why?

DECLAREv_employee_id employees.employee_id%TYPE;v_last_name employees.last_name%TYPE;

Page 2 of 4

Page 3: plsql_07_01_handling_exceptions.doc

Your Name PL/SQL 07-01 Date Submitted

BEGINSELECT employee_id, last_name INTO v_employee_id, v_last_name

FROM employeesWHERE department_id = 10; -- also run for 20 and for 30

DBMS_OUTPUT.PUT_LINE(‘The SELECT was successful’);EXCEPTION

WHEN OTHERS THENDBMS_OUTPUT.PUT_LINE(‘An exception has occurred’);

END;

10 - The SELECT was successful

Statement processed.

10 worked because there is exactly 1 employee in that department

20 - An exception has occurred

Statement processed.

20 did not work because there is more than one employee in that department

30 - An exception has occurred

Statement processed.

30 did not work because there is no employee in that department

Page 3 of 4

Page 4: plsql_07_01_handling_exceptions.doc

Your Name PL/SQL 07-01 Date Submitted

7. Modify your code from question 6 to add two more exception handlers to trap the possibleexceptions individually. Use NO_DATA_FOUND and TOO_MANY_ROWS. Re-run theblock three times, using 10, 20 and 30 as before. Observe the message displayed in eachcase.

DECLAREv_employee_id employees.employee_id%TYPE;v_last_name employees.last_name%TYPE;BEGINSELECT employee_id, last_name INTO v_employee_id, v_last_nameFROM employeesWHERE department_id = 10;DBMS_OUTPUT.PUT_LINE('The SELECT was successful');EXCEPTIONWHEN NO_DATA_FOUND THENDBMS_OUTPUT.PUT_LINE('No rows were selected');WHEN TOO_MANY_ROWS THENDBMS_OUTPUT.PUT_LINE('More than one row was selected');WHEN OTHERS THENDBMS_OUTPUT.PUT_LINE('An exception has occurred');END;

10 - The SELECT was successful

Statement processed.

10 worked because there is exactly 1 employee in that department

20 - More than one row was selected

Statement processed.

20 has been handled with an exception handler

30 - No rows were selected

Statement processed.

30 has been handled with an exception handler

8. List three guidelines for trapping exceptions.

Always add exception handlers whenever there is possibility of an error occurring. Handle named exceptions whenever possible instead of using OTHERS in exception handlers Write out debugging information in your exception handler

Page 4 of 4