PLSQL 7.1 DL
PLSQL 7.1 DL
PLSQL 7.1 DL
Exception Handler Code that defines the recovery actions to be performed when
execution-time errors occur.
Try It / Solve It
1. What happens when Oracle encounters a runtime problem while executing a PL/SQL block?
Nos va a mostrar una excepción.
2. What do you need to add to your PL/SQL block to address these problems?
Podemos manejar más faciles los erroes, evitamos que truene el programa y sabemos además
cual es el error que se presenta.
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
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
lOMoARcPSD|30705043
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.
Truena el programa porque hay más filas de las que se solicitan ya que en el department_id
80 hay varios empleados, esto lo podemos solucionar con una excepción.
DECLARE
v_jobid employees.job_id%TYPE;
BEGIN
SELECT job_id INTO v_jobid
FROM employees
WHERE department_id = 80;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Demasiadas filas');
END;
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;
Muestra el mensaje de que ocurrió una excepción porque el id 50 ya existe.
6. Enter the following code to create a copy of the employees table for this and the
next question.
In the new emp_temp table, delete all but one of the employees in department 10.
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 emp_temp.employee_id%TYPE;
v_last_name emp_temp.last_name%TYPE;
BEGIN
SELECT employee_id, last_name INTO v_employee_id, v_last_name
FROM emp_temp
Con el 10 se va a ejecutar bien solo que sale la excepción que se ejecuto porque hay
muchas filas, con el 20 pasa lo mismo y con el 30 sale un error porque el id no existe.
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.
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.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
lOMoARcPSD|30705043
DECLARE
v_number
NUMBER(2); BEGIN
v_number := 9999;
EXCEPTION
WHEN OTHERS THEN
Se ejecuta correctamente y muestra dos mensajes que dicen que ha ocurrido una excepción y un
número por el tipo de dato de variable y el otro por variable global.
10. Enter and run the following code and explain the output.
DECLARE
v_number NUMBER(4);
BEGIN
v_number := 1234;
DECLARE
v_number NUMBER(4);
BEGIN
v_number := 5678;
v_number := 'A character string';
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An exception has occurred');
DBMS_OUTPUT.PUT_LINE('The number is: ' || v_number);
END;
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.