Section 7 Lesson 3: Trapping User-Defined Exceptions: Vocabulary
Section 7 Lesson 3: Trapping User-Defined Exceptions: Vocabulary
Vocabulary
Identify the vocabulary word for each definition below:
RAISE_APPLICATION_ERROR
A procedure used to return user-defined error messages from
stored subprograms.
RAISE statement
Use this statement to raise a named exception.
User defined errors
These errors are not automatically rased by the racle
!erver" but are defined by the programmer and are specific
to the programmer#s code.
Try It Sol!e It
All the $uestions in this exercise use a copy of the employees table. %reate this copy
by running the following !&' statement:
%()AT) TA*') excep+emps A! !)')%T , -(. employees/
0. %reate a 1'2!&' bloc3 that updates the salary of every employee to a new value of
04444 in a chosen 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 !&'%5) and !&')((.. Test your code three times"
using department+ids 64" 74" and 84.
5)%'A()
e+no+rows+updated )9%)1TI:/
v+deptno :U.*)( :; 64/
*)<I:
U15AT) excep+emps
!)T salary ; 04444
=>)() department+id ; v+deptno/
I- !&'?:T-U:5 T>):
(AI!) e+no+rows+updated/
):5 I-/
%..IT/
)9%)1TI:
=>): e+no+rows+updated T>):
5*.!+UT1UT.1UT+'I:) @ #There are no employees in that department.#A/
=>): T>)(! T>):
5*.!+UT1UT.1UT+'I:)@#An error has occurred:#BB!&'%5) BB #-# BB !&')((.A/
):5/
%opyright C 6407" racle and2or its affiliates. All rights reserved. racle and Dava are registered trademar3s of racle and2or its affiliates. ther names may be trademar3s of their respective
owners.
1entru department+id;64
1entru department+id;74
1entru department+id;84
6. .odify your code from $uestion 6 to handle the condition where no rows are updated
using (AI!)+A11'I%ATI:+)((( procedure in the exception section. Use an error
number of E64646. Test your code again using department+id 84 and chec3 that the E
64646 error is displayed.
5)%'A()
e+no+rows+updated )9%)1TI:/
v+deptno :U.*)( :; 84/
*)<I:
U15AT) excep+emps
!)T salary ; 04444
=>)() department+id ; v+deptno/
I- !&'?:T-U:5 T>):
(AI!) e+no+rows+updated/
):5 I-/
%..IT/
)9%)1TI:
=>): e+no+rows+updated T>):
(AI!)+A11'I%ATI:+)(((@-64646"#There are no employees in that department#A/
=>): T>)(! T>):
5*.!+UT1UT.1UT+'I:)@#An error has occurred:#BB!&'%5) BB #-# BB !&')((.A/
):5/
%opyright C 6407" racle and2or its affiliates. All rights reserved. racle and Dava are registered trademar3s of racle and2or its affiliates. ther names may be trademar3s of their respective
owners.
7. .odify your code from $uestion 7 to use (AI!)+A11'I%ATI:+)((( in the
executable section instead of the exception section. Test your code again
using department+id 84.
5)%'A()
e+no+rows+updated )9%)1TI:/
v+deptno :U.*)( :; 84/
*)<I:
U15AT) excep+emps
!)T salary ; 04444
=>)() department+id ; v+deptno/
I- !&'?:T-U:5 T>):
(AI!)+A11'I%ATI:+)(((@-64646"#There are no employees in that
department#A/
):5 I-/
%..IT/
):5/
%opyright C 6407" racle and2or its affiliates. All rights reserved. racle and Dava are registered trademar3s of racle and2or its affiliates. ther names may be trademar3s of their respective
owners.
8. *efore starting this $uestion" disable Autocommit in Application )xpress.
A. )nter and run the following 1'2!&' bloc3 using department+id ; 84" and explain
the output.
5)%'A()
v+dept+id excep+emps.department+id?TF1)/
v+count :U.*)(/
*)<I:
v+dept+id :; 84/
!)')%T %U:T@,A I:T v+count
-(. excep+emps
=>)() department+id ; v+dept+id/
5*.!+UT1UT.1UT+'I:)@#There are # BB v+count BB # employees#A/
5)')T) -(. excep+emps
=>)() department+id ; v+dept+id/
5*.!+UT1UT.1UT+'I:)@!&'?(=%U:T BB
# employees were deleted#A/
(''*A%G/
):5/
:u avem nici o eroare chiar daca nu exista angaHati in acel departament.
*. .odify your bloc3 to include two user-defined exception handlers" one to test whether
!)')%T returns a value of 4" and the other to test if no rows were 5)')T)d.
5eclare the exceptions and (AI!) them explicitly before trapping them in the
exception section. 5o :T use (AI!)+A11'I%ATI:+)(((. Test your modified
bloc3
using department+id 84.
5)%'A()
v+dept+id excep+emps.department+id?TF1)/
v+count :U.*)(/
e+no+emps+in+dept )9%)1TI:/
e+no+rows+deleted )9%)1TI:/
*)<I:
%opyright C 6407" racle and2or its affiliates. All rights reserved. racle and Dava are registered trademar3s of racle and2or its affiliates. ther names may be trademar3s of their
respective owners.
v+dept+id :; 84/
!)')%T %U:T@,A I:T v+count
-(. excep+emps
=>)() department+id ; v+dept+id/
I- v+count ; 4
T>): (AI!) e+no+emps+in+dept/
):5 I-/
5*.!+UT1UT.1UT+'I:)@#There are # BB v+count BB # employees#A/
5)')T) -(. excep+emps
=>)() department+id ; v+dept+id/
I- !&'?:T-U:5 T>):
(AI!) e+no+rows+deleted/
):5 I-/
5*.!+UT1UT.1UT+'I:)@!&'?(=%U:T BB# employees were deleted#A/
(''*A%G/
)9%)1TI:
=>): e+no+emps+in+dept T>):
5*.!+UT1UT.1UT+'I:)@#:u sunt angaHati in acest departament#A/
=>): e+no+rows+deleted T>):
5*.!+UT1UT.1UT+'I:) @#:ici un rand nu a fost sters#A/
):5/
%. .odify your bloc3 again to use (AI!)+A11'I%ATI:+)((( in the executable
section. Use error numbers E64647 and E64648. Test your modified bloc3 using
department+id 84.
5)%'A()
v+dept+id excep+emps.department+id?TF1)/
v+count :U.*)(/
*)<I:
v+dept+id :; 84/
!)')%T %U:T@,A I:T v+count
-(. excep+emps
=>)() department+id ; v+dept+id/
I- v+count ; 4 T>):
(AI!)+A11'I%ATI:+)((( @-64647"#:u exista angaHati in acest departament#A/
):5 I-/
5*.!+UT1UT.1UT+'I:)@#There are # BB v+count BB # employees#A/
5)')T) -(. excep+emps
=>)() department+id ; v+dept+id/
I- !&'?:T-U:5 T>):
(AI!)+A11'I%ATI:+)(((@-64648"#:ici un angaHat nu a fost sters#A/
):5 I-/
5*.!+UT1UT.1UT+'I:)@!&'?(=%U:T BB# employees were deleted#A/
%opyright C 6407" racle and2or its affiliates. All rights reserved. racle and Dava are registered trademar3s of racle and2or its affiliates. ther names may be trademar3s of their
respective owners.
(''*A%G/
):5/
%opyright C 6407" racle and2or its affiliates. All rights reserved. racle and Dava are registered trademar3s of racle and2or its affiliates. ther names may be trademar3s of their
respective owners.