1
2
3
4
5import java.io.IOException;
6
7
8
9public
10class ExceptionTest {
11
12 public
13 static
14 void main(String[] argv) {
15 int i = 0;
16
17 try {
18 i = Integer.parseInt(argv[0]);
19 }
20 catch ( Exception e ) { }
21
22 test(i);
23 }
24
25
26
27 public
28 static
29 void test(int i) {
30 System.out.println("test: case = " + i);
31
32
33
34 tryblock:
35 try {
36 switch ( i ) {
37
38
39 case 1:
40 System.out.println("test: return inside try block");
41 return;
42
43
44 case 2:
45 System.out.println("test: break out of try block");
46 break tryblock;
47
48
49 case 3:
50 System.out.println(
51 "test: throw new Exception(\"a test\")");
52 throw
53 new Exception("a test");
54
55
56 case 4:
57 System.out.println(
58 "test: throw new MyException(\"my test\")");
59 throw
60 new MyException("my test");
61
62
63 case 5:
64 System.out.println("test: call foo");
65 foo();
66 break;
67
68
69 case 6:
70 System.out.println("test: call bar");
71 bar();
72 break;
73
74 case 7:
75 System.out.println("test: call wow");
76 wow();
77 break;
78
79 case 8:
80 System.out.println("test: call tee with 10");
81 tee(10);
82 break;
83
84 case 9:
85 System.out.println("test: call bad");
86 bad();
87 break;
88
89 default:
90 System.out.println("test: normal exit of try block");
91
92 }
93
94 }
95
96
97
98 catch ( MyException e ) {
99 Throwable cause;
100 System.out.println(
101 "test: caught MyException: " + e
102 + ( (cause = e.getCause()) != null
103 ? " cause: " + cause
104 : ""
105 )
106 );
107 e.printStackTrace(System.out);
108 }
109
110
111 catch ( RuntimeException e ) {
112 System.out.println(
113 "test: caught RuntimeException: " + e);
114 e.printStackTrace(System.out);
115 }
116
117
118 catch ( Exception e ) {
119 System.out.println(
120 "test: caught Exception: " + e);
121 e.printStackTrace(System.out);
122 }
123
124
125 finally {
126 System.out.println(
127 "test: exec finally clause");
128 }
129
130
131
132 System.out.println("test: normal exit");
133 }
134
135
136
137 public
138 static
139 void foo()
140 throws MyException {
141
142 try {
143 System.out.println(
144 "foo : throw new MyException(\"foo test\")");
145
146 throw
147 new MyException("foo test");
148 }
149 finally {
150 System.out.println("foo : exec finally clause");
151 }
152 }
153
154
155
156 public static
157 void bar() {
158 System.out.println(
159 "bar : throw new IllegalArgumentException(\"bar test\")");
160
161 throw
162 new IllegalArgumentException("bar test");
163 }
164
165
166
167 public static
168 void wow()
169 throws MyException {
170 int a, b=1, c=0;
171
172 try {
173 System.out.println(
174 "wow : division by 0");
175 a = b/c;
176 }
177 catch (Exception e) {
178 System.out.println(
179 "wow : caught Exception: " + e);
180 System.out.println(
181 "wow : throw new MyException(\"wow test\")");
182
183 throw
184 new MyException("wow test", e);
185 }
186 }
187
188
189
190 public
191 static
192 void tee(int i)
193 throws MyException {
194
195 try {
196
197 System.out.println(
198 "tee : called with " + i);
199
200 if ( i <= 0 ) {
201 System.out.println(
202 "tee : throw new MyException(\"tee test\")");
203
204 throw
205 new MyException("tee test");
206 }
207 else {
208 tee(i-1);
209 }
210 }
211 finally {
212 System.out.println("tee : exec finally clause, i = " + i);
213 }
214 }
215
216 public static
217 void bad()
218 throws MyException {
219
220 try {
221 System.out.println(
222 "bad: throw new IOException(\"bad test\")");
223 throw
224 new IOException("bad test");
225 }
226 finally {
227 System.out.println("bad: exec finally clause");
228 System.out.println(
229 "bad: throw new MyException(\"bad finally test\")");
230
231 throw
232 new MyException("bad finally test");
233 }
234 }
235}
236
237
238
239class MyException extends Exception {
240
241 public
242 MyException(String s) {
243 super(s);
244 }
245
246 public
247 MyException(String s, Throwable cause) {
248 super(s, cause);
249 }
250
251}
252
253