- Notifications
You must be signed in to change notification settings - Fork 849
/
Copy pathexception-in-declaration.sql
73 lines (66 loc) · 1.85 KB
/
exception-in-declaration.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
This sometimes surprises a developer new to PL/SQL. The exception section of a
PL/SQL block can only possibly handle an exception raised in the executable section.
An exception raised in the declaration section (in an attempt to assign a default
value to a variable or constant) always propagates out unhandled to the enclosing block.
*/
-- Error Raised in Declaration Section - Not Handled
DECLARE
aname VARCHAR2 (5) :='Big String';
BEGIN
DBMS_OUTPUT.put_line (aname);
EXCEPTION
WHEN VALUE_ERROR
THEN
DBMS_OUTPUT.put_line ('Handled!');
END;
/
-- Trapped in Outer Block
-- Can't trap the exception in the same block (when raised in the declaration section),
-- but certainly it is trappable in an outer block.
BEGIN
DECLARE
aname VARCHAR2 (5) :='Big String';
BEGIN
DBMS_OUTPUT.put_line (aname);
EXCEPTION
WHEN VALUE_ERROR
THEN
DBMS_OUTPUT.put_line ('Handled!');
END;
EXCEPTION
WHEN VALUE_ERROR
THEN
DBMS_OUTPUT.put_line ('Handled in outer block!');
END;
/
-- What's a Developer to Do? Initialize Later!
/*
Generally, I recommend that you created a nested subprogram called
"initialize" and move all of your initialization into that procedure.
Then call it as the first line in your "main" subprogram. That way,
an exception raised when assigning a value can be trapped in that
subprogram's exception section.
*/
BEGIN
DECLARE
aname VARCHAR2 (5);
PROCEDURE initialize
IS
BEGIN
aname :='Big String'; -- pkg.func ();
END;
BEGIN
initialize;
DBMS_OUTPUT.put_line (aname);
EXCEPTION
WHEN VALUE_ERROR
THEN
DBMS_OUTPUT.put_line ('Handled!');
END;
EXCEPTION
WHEN VALUE_ERROR
THEN
DBMS_OUTPUT.put_line ('Handled in outer block!');
END;
/