0% found this document useful (0 votes)
162 views3 pages

Lab Exer 5

When a runtime error occurs in a PL/SQL block, an exception is raised. To handle exceptions, exception handlers using keywords like WHEN OTHERS or specific exceptions like NO_DATA_FOUND or TOO_MANY_ROWS need to be added. Handling exceptions allows errors to be gracefully handled rather than causing program failure. Specific guidelines for exception handling include only handle exceptions that are expected, re-raise exceptions after handling, and log exception details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views3 pages

Lab Exer 5

When a runtime error occurs in a PL/SQL block, an exception is raised. To handle exceptions, exception handlers using keywords like WHEN OTHERS or specific exceptions like NO_DATA_FOUND or TOO_MANY_ROWS need to be added. Handling exceptions allows errors to be gracefully handled rather than causing program failure. Specific guidelines for exception handling include only handle exceptions that are expected, re-raise exceptions after handling, and log exception details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Try It / Solve It

1. What happens when Oracle encounters a runtime problem while executing a PL/SQL
block?

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

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

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;

A. What happens when you run the block? In your own words, explain what you can do
to fix this problem.

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

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

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 THEN DBMS_OUTPUT.PUT_LINE ('An exception


has occurred.'); END;

6. Enter the following PL/SQL block, which tries to SELECT all the employees in a
specific department. Run it three times, using department_ids 10, 20, and 30. What
happens and why?

DECLARE

v_employee_id employees.employee_id%TYPE;

v_last_name employees.last_name%TYPE;

BEGIN

SELECT employee_id, last_name INTO v_employee_id, v_last_name FROM


employees

WHERE department_id = 10; -- run with values 10, 20, and 30

DBMS_OUTPUT.PUT_LINE('The SELECT was successful');

EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An exception


has occurred');

END;

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

8. List three guidelines for trapping exceptions.


9. Enter and run the following PL/SQL block. Explain the output. Note: the WHEN
OTHERS handler successfully handles any type of exception which occurs.
DECLARE v_number NUMBER(2); BEGIN v_number := 9999;
EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An exception
has occurred'); END;

10. Modify the block in question 9 to omit the exception handler, then re-run the block.
Explain the output.

You might also like