0% found this document useful (0 votes)
7 views4 pages

PLSQL 7.1 DL

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

lOMoARcPSD|30705043

Database Programming with PL/SQL7-1: Handling Exceptions


Practice Activities
Vocabulary
Identify the vocabulary word for each definition below:
academy.oracle.com

Exception Handler Code that defines the recovery actions to be performed when
execution-time errors occur.

Exception 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?
Nos va a mostrar una excepción.

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

Debemos utilizar una excepción

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

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.

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


handler.

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;

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


Se imprime la excepción que hay demasiadas filas en nuestra tabla.

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.

CREATE TABLE emp_temp AS SELECT * FROM employees;


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

In the new emp_temp table, delete all but one of the employees in department 10.

SELECT * FROM emp_temp WHERE department_id = 10;

DELETE FROM emp_temp WHERE employee_id = …; (repeat as necessary)

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

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;

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.

When finished, remember to delete the emp_temp table.

DROP TABLE emp_temp;

8. List three guidelines for trapping exceptions.


Siempre hay que agregar exepciones para cuando ocurra un error.
Hay que aprendernos los nombres y las causas de las excepciones predefinidas
Hay qie manejar las excepciones con nombre siempre que sea posible.

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

DBMS_OUTPUT.PUT_LINE('An exception has


occurred'); END;

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.

You might also like