You are on page 1of 6

www.oracle.

com/academy

Database Programming with PL/SQL


7-3: Trapping User-Defined Exceptions Practice
Activities

Vocabulary

Identify the vocabulary word for each definition below:

m
er as
co
eH w
RAISE_APPLICATION_ERROR A procedure used to return user-defined error messages from
stored subprograms.

o.
rs e
ou urc
RAISE Use this statement to raise a named exception.

User-Defined error These errors are not automatically raısed by the Oracle Server,
o

but are defined by the programmer and are specific to the


programmer's code.
aC s
vi y re

Try It / Solve It
ed d
ar stu

All the questions in this exercise use a copy of the employees table. Create this copy by running the
following SQL statement:
is

CREATE TABLE excep_emps AS SELECT * FROM employees;


Th

Table created.
1. Create a PL/SQL block that updates the salary of every employee to a new value of 10000 in a chosen
sh

department. Include a user-defined exception handler that handles the condition where no rows are
updated and displays a custom message. Also include an exception handler that will trap any other
possible error condition and display the corresponding SQLCODE and SQLERRM. Test your code
three times, using department_ids 20, 30, and 40.

Copyright © 2018, 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.
This study source was downloaded by 100000809939014 from CourseHero.com on 06-30-2021 13:06:45 GMT -05:00

https://www.coursehero.com/file/38233982/PLSQL-7-3-Practice-HW-DONEdocx/
DECLARE
e_no_rows_updated EXCEPTION;
BEGIN
UPDATE excep_emps
SET salary = 10000
WHERE department_id = 20;
IF SQL%NOTFOUND THEN -- or we can say: IF SQL%ROWCOUNT = 0
RAISE e_no_rows_updated;
END IF;
EXCEPTION
WHEN e_no_rows_updated THEN
DBMS_OUTPUT.PUT_LINE ( 'There are no employees in that department.');

m
er as
WHEN OTHERS THEN

co
DBMS_OUTPUT.PUT_LINE('An error has occurred:'||SQLCODE || '-' || SQLERRM);

eH w
END;

o.
Statement processed. rs e
ou urc
DECLARE
e_no_rows_updated EXCEPTION;
o

BEGIN
aC s
vi y re

UPDATE excep_emps
SET salary = 10000
WHERE department_id = 30;
ed d
ar stu

IF SQL%NOTFOUND THEN -- or we can say: IF SQL%ROWCOUNT = 0


RAISE e_no_rows_updated;
END IF;
is

EXCEPTION
Th

WHEN e_no_rows_updated THEN


DBMS_OUTPUT.PUT_LINE ( 'There are no employees in that department.');
sh

WHEN OTHERS THEN


DBMS_OUTPUT.PUT_LINE('An error has occurred:'||SQLCODE || '-' || SQLERRM);

Copyright © 2018, 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.
This study source was downloaded by 100000809939014 from CourseHero.com on 06-30-2021 13:06:45 GMT -05:00

https://www.coursehero.com/file/38233982/PLSQL-7-3-Practice-HW-DONEdocx/
END;
There are no employees in that department.
Statement processed.
DECLARE
e_no_rows_updated EXCEPTION;
BEGIN
UPDATE excep_emps
SET salary = 10000
WHERE department_id = 40;
IF SQL%NOTFOUND THEN -- or we can say: IF SQL%ROWCOUNT = 0
RAISE e_no_rows_updated;
END IF;

m
er as
EXCEPTION

co
WHEN e_no_rows_updated THEN

eH w
DBMS_OUTPUT.PUT_LINE ( 'There are no employees in that department.');

o.
WHEN OTHERS THEN rs e
ou urc
DBMS_OUTPUT.PUT_LINE('An error has occurred:'||SQLCODE || '-' || SQLERRM);
END;
o

There are no employees in that department.


aC s
vi y re

Statement processed.

2. Modify your code from question 1 to handle the condition where no rows are updated using
ed d

RAISE_APPLICATION_ERROR procedure in the exception section. Use an error number of –


ar stu

20202. Test your code again using department_id 40 and check that the
–20202 error is displayed.
is

DECLARE
Th

e_no_rows_updated EXCEPTION;
BEGIN
UPDATE excep_emps
sh

SET salary = 10000


WHERE department_id = 40;

Copyright © 2018, 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.
This study source was downloaded by 100000809939014 from CourseHero.com on 06-30-2021 13:06:45 GMT -05:00

https://www.coursehero.com/file/38233982/PLSQL-7-3-Practice-HW-DONEdocx/
IF SQL%NOTFOUND THEN
RAISE e_no_rows_updated;
END IF;
EXCEPTION
WHEN e_no_rows_updated THEN
RAISE_APPLICATION_ERROR(-20202,'There are no employees in that department');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error has occurred: '||SQLCODE || '-' || SQLERRM);
END;
ORA-20202: There are no employees in that department

3. Modify your code from question 2 to use RAISE_APPLICATION_ERROR in the executable section
instead of the exception section. Test your code again using department_id 40.
DECLARE

m
er as
e_no_rows_updated EXCEPTION;

co
BEGIN

eH w
UPDATE excep_emps

o.
SET salary = 10000
rs e
WHERE department_id = 40;
ou urc
IF SQL%NOTFOUND THEN
RAISE_APPLICATION_ERROR(-20202,'There are no employees in that
department');
o

END IF;
aC s
vi y re

END;
ORA-20202: There are no employees in that department
ed d

4. Be careful incorporating DELETE statements in PL/SQL blocks. If you make a mistake, you may
ar stu

inadvertently delete data that you didn't mean to delete.


is

A. Enter and run the following PL/SQL block using department_id = 40, and explain the output.
Th

DECLARE
v_dept_id excep_emps.department_id%TYPE;
sh

v_count NUMBER; BEGIN


v_dept_id := 40;
SELECT COUNT(*) INTO v_count
FROM excep_emps

Copyright © 2018, 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.
This study source was downloaded by 100000809939014 from CourseHero.com on 06-30-2021 13:06:45 GMT -05:00

https://www.coursehero.com/file/38233982/PLSQL-7-3-Practice-HW-DONEdocx/
WHERE department_id = v_dept_id;
DBMS_OUTPUT.PUT_LINE('There are ' || v_count || ' employees');
DELETE FROM excep_emps
WHERE department_id = v_dept_id;
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' employees were deleted');
ROLLBACK;
END;
There are 0 employees
1 employees were deleted
1 row(s) deleted.

B. Modify your code to include two user-defined exception handlers, one to test whether SELECT returns
a value of 0, and the other to test if no rows were DELETEd. Declare the exceptions and RAISE them
explicitly before trapping them in the EXCEPTION section. Do NOT use
RAISE_APPLICATION_ERROR. Test your modified block using department_id 40.
DECLARE
v_dept_id excep_emps.department_id%TYPE;
v_count NUMBER;

m
er as
e_no_emps_in_dept EXCEPTION;
e_no_rows_deleted EXCEPTION;

co
BEGIN

eH w
v_dept_id := 40;

o.
SELECT COUNT(*) INTO v_count
FROM excep_emps rs e
ou urc
WHERE department_id = v_dept_id;
IF v_count = 0 THEN
RAISE e_no_emps_in_dept;
o

END IF;
aC s

DBMS_OUTPUT.PUT_LINE('There are ' || v_count || ' employees');


vi y re

DELETE FROM excep_emps


WHERE department_id = v_dept_id;
IF SQL%NOTFOUND THEN -- or IF SQL%ROWCOUNT = 0 THEN
RAISE e_no_rows_deleted;
ed d

END IF;
ar stu

DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT ||' employees were deleted');


ROLLBACK;
EXCEPTION
WHEN e_no_emps_in_dept THEN
is

DBMS_OUTPUT.PUT_LINE('This department has no employees');


Th

WHEN e_no_rows_deleted THEN


DBMS_OUTPUT.PUT_LINE('No employees were deleted');
END;
sh

This department has no employees

Statement processed.

Copyright © 2018, 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.
This study source was downloaded by 100000809939014 from CourseHero.com on 06-30-2021 13:06:45 GMT -05:00

https://www.coursehero.com/file/38233982/PLSQL-7-3-Practice-HW-DONEdocx/
C. Modify your block again to use RAISE_APPLICATION_ERROR in the executable section. Use error
numbers –20203 and –20204. Test your modified block using department_id 40.
DECLARE
v_dept_id excep_emps.department_id%TYPE;
v_count NUMBER;
BEGIN
v_dept_id := 40;
SELECT COUNT(*) INTO v_count
FROM excep_emps
WHERE department_id = v_dept_id;
IF v_count = 0 THEN
RAISE_APPLICATION_ERROR(-20203,'This department has no employees');

m
END IF;

er as
co
DBMS_OUTPUT.PUT_LINE('There are ' || v_count || ' employees');

eH w
DELETE FROM excep_emps

o.
WHERE department_id = v_dept_id;
rs e
ou urc
IF SQL%NOTFOUND THEN -- sau IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20204,'No employees were deleted');
o

END IF;
aC s
vi y re

DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT ||' employees were deleted');


ROLLBACK;
END;
ed d

ORA-20203: This department has no employees


ar stu
is
Th
sh

Copyright © 2018, 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.
This study source was downloaded by 100000809939014 from CourseHero.com on 06-30-2021 13:06:45 GMT -05:00

https://www.coursehero.com/file/38233982/PLSQL-7-3-Practice-HW-DONEdocx/
Powered by TCPDF (www.tcpdf.org)

You might also like