Skip to content

Commit c800e34

Browse files
author
Steven Feuerstein
authored
Create raising-exceptions
I would like to load a lot of the scripts I have created at LiveSQL to this examples repo as well. Please let me know if this format/approach needs to be changed in any way.
1 parent 41745f2 commit c800e34

File tree

1 file changed

+207
-0
lines changed

1 file changed

+207
-0
lines changed
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
An exploration into RAISE and RAISE_APPLICATION_ERROR
3+
*/
4+
5+
-- Explicit Raise of System Exception
6+
CREATE OR REPLACE PROCEDURE use_salary (salary_in IN NUMBER)
7+
IS
8+
BEGIN
9+
IF salary_in < 0
10+
THEN
11+
RAISE VALUE_ERROR;
12+
END IF;
13+
END;
14+
/
15+
16+
BEGIN
17+
use_salary (salary_in => -1);
18+
END;
19+
/
20+
21+
-- Raise a User-Defined Exception
22+
-- Which in this case simply "mimics" the pre-defined VALUE_ERROR exception and doesn't really add value.
23+
CREATE OR REPLACE PROCEDURE use_salary (salary_in IN NUMBER)
24+
IS
25+
negative_salary EXCEPTION;
26+
PRAGMA EXCEPTION_INIT (negative_salary, -6502);
27+
BEGIN
28+
IF salary_in < 0
29+
THEN
30+
RAISE negative_salary;
31+
END IF;
32+
END;
33+
/
34+
35+
BEGIN
36+
use_salary (salary_in => -1);
37+
END;
38+
/
39+
40+
-- Different Code for Different Error Conditions
41+
-- All the exception handling logic "front loaded" into the executable section, making it harder to focus on the "positive" side of things: when nothing went wrong. Not a great way to write maintainable code.
42+
CREATE OR REPLACE PROCEDURE use_salary (salary_in IN NUMBER)
43+
IS
44+
PROCEDURE notify_support (string_in IN VARCHAR2)
45+
IS
46+
BEGIN
47+
-- Just a placeholder to make a point!
48+
DBMS_OUTPUT.PUT_LINE ('Hey support, deal with THIS: ' || string_in);
49+
END;
50+
51+
PROCEDURE notify_hr (string_in IN VARCHAR2)
52+
IS
53+
BEGIN
54+
-- Just a placeholder to make a point!
55+
DBMS_OUTPUT.PUT_LINE ('Hey HR, deal with THIS: ' || string_in);
56+
END;
57+
BEGIN
58+
CASE
59+
WHEN salary_in < 0
60+
THEN
61+
notify_support (
62+
'Negative salary submitted ' || salary_in);
63+
RAISE VALUE_ERROR;
64+
WHEN salary_in > 10000
65+
THEN
66+
notify_support (
67+
'Too large salary submitted ' || salary_in);
68+
RAISE VALUE_ERROR;
69+
WHEN salary_in < 100
70+
THEN
71+
notify_hr (
72+
'No one should be treated so shabbily! ' || salary_in);
73+
RAISE VALUE_ERROR;
74+
ELSE
75+
/* No problems, proceed with normal execution*/
76+
NULL;
77+
END CASE;
78+
79+
/* Rest of procedure */
80+
END;
81+
/
82+
83+
BEGIN
84+
use_salary (salary_in => -1);
85+
END;
86+
/
87+
88+
-- Move Exception Handling Logic to Exception Section
89+
-- Cleaner and easier to understand, debug and maintain.
90+
CREATE OR REPLACE PROCEDURE use_salary (salary_in IN NUMBER)
91+
IS
92+
negative_salary EXCEPTION;
93+
too_large_salary EXCEPTION;
94+
too_small_salary EXCEPTION;
95+
96+
PROCEDURE notify_support (string_in IN VARCHAR2)
97+
IS
98+
BEGIN
99+
-- Just a placeholder to make a point!
100+
DBMS_OUTPUT.PUT_LINE ('Hey support, deal with THIS: ' || string_in);
101+
END;
102+
103+
PROCEDURE notify_hr (string_in IN VARCHAR2)
104+
IS
105+
BEGIN
106+
-- Just a placeholder to make a point!
107+
DBMS_OUTPUT.PUT_LINE ('Hey HR, deal with THIS: ' || string_in);
108+
END;
109+
BEGIN
110+
CASE
111+
WHEN salary_in < 0 THEN RAISE negative_salary;
112+
WHEN salary_in > 10000 THEN RAISE too_large_salary;
113+
WHEN salary_in < 100 THEN RAISE too_small_salary;
114+
ELSE NULL;
115+
END CASE;
116+
117+
/* Rest of procedure */
118+
119+
EXCEPTION
120+
WHEN negative_salary
121+
THEN
122+
notify_support (
123+
'Negative salary submitted ' || salary_in);
124+
RAISE VALUE_ERROR;
125+
126+
WHEN too_large_salary
127+
THEN
128+
notify_support (
129+
'Too large salary submitted ' || salary_in);
130+
RAISE VALUE_ERROR;
131+
132+
WHEN too_small_salary
133+
THEN
134+
notify_hr (
135+
'No one should be treated so shabbily! ' || salary_in);
136+
RAISE VALUE_ERROR;
137+
END;
138+
/
139+
140+
BEGIN
141+
use_salary (salary_in => -1);
142+
END;
143+
/
144+
145+
-- Use RAISE_APPLICATION_ERROR for App-Specific Error Message
146+
BEGIN
147+
RAISE_APPLICATION_ERROR (-20000, 'Say whatever you want');
148+
END;
149+
/
150+
151+
CREATE OR REPLACE PROCEDURE use_salary (salary_in IN NUMBER)
152+
IS
153+
negative_salary EXCEPTION;
154+
too_large_salary EXCEPTION;
155+
too_small_salary EXCEPTION;
156+
157+
PROCEDURE notify_support (string_in IN VARCHAR2)
158+
IS
159+
BEGIN
160+
-- Just a placeholder to make a point!
161+
DBMS_OUTPUT.PUT_LINE ('Hey support, deal with THIS: ' || string_in);
162+
END;
163+
164+
PROCEDURE notify_hr (string_in IN VARCHAR2)
165+
IS
166+
BEGIN
167+
-- Just a placeholder to make a point!
168+
DBMS_OUTPUT.PUT_LINE ('Hey HR, deal with THIS: ' || string_in);
169+
END;
170+
BEGIN
171+
CASE
172+
WHEN salary_in < 0 THEN RAISE negative_salary;
173+
WHEN salary_in > 10000 THEN RAISE too_large_salary;
174+
WHEN salary_in < 100 THEN RAISE too_small_salary;
175+
ELSE NULL;
176+
END CASE;
177+
178+
/* Rest of procedure */
179+
180+
EXCEPTION
181+
WHEN negative_salary
182+
THEN
183+
notify_support (
184+
'Negative salary submitted ' || salary_in);
185+
RAISE_APPLICATION_ERROR (-20001,
186+
'Negative salaries are not allowed. Please re-enter.');
187+
188+
WHEN too_large_salary
189+
THEN
190+
notify_support (
191+
'Too large salary submitted ' || salary_in);
192+
RAISE_APPLICATION_ERROR (-20001,
193+
'We are not nearly that generous. Please re-enter.');
194+
195+
WHEN too_small_salary
196+
THEN
197+
notify_hr (
198+
'No one should be treated so shabbily! ' || salary_in);
199+
RAISE_APPLICATION_ERROR (-20001,
200+
'C''mon, a person''s gotta eat! Please re-enter.');
201+
END;
202+
/
203+
204+
BEGIN
205+
use_salary (salary_in => -1);
206+
END;
207+
/

0 commit comments

Comments
 (0)