forked from cirosantilli/java-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionCheat.java
More file actions
236 lines (182 loc) · 6.5 KB
/
ExceptionCheat.java
File metadata and controls
236 lines (182 loc) · 6.5 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
# Exceptions
*/
import java.io.Closeable;
public class ExceptionCheat {
public static void main(String[] args) throws Exception {
/*
# try
Catches exceptions.
*/
{
/*
# Closeable
# try with resources
Java 7 Project Coin feature.
http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
Try with parenthesis.
Automatically calls `close()` on variables defined there,
which must implement Closeable, which thus has a magic language function.
Major use case: file IO, to ensure that the file gets closed.
Similar to the with construct in Python which is often used for the same use case.
A similar effect can be achieved with a `finally` block,
although handling exceptions in the file opening may be trickier.
*/
{
try(
CloseableClass c = new CloseableClass();
CloseableClass c2 = new CloseableClass()
) {
assert CloseableClass.i == 0;
// Dummy usage to avoid warnings.
assert c != null;
assert c2 != null;
}
assert CloseableClass.i == 2;
}
}
/*
# catch
*/
{
/*
# catch multiple types of exceptions
Java 7 Coin Project.
`|` operator... ugly.
http://stackoverflow.com/questions/3495926/can-i-catch-multiple-java-exceptions-in-the-same-catch-clause
*/
{
try {
throw new IllegalArgumentException();
} catch (NullPointerException | IllegalArgumentException e) {}
}
}
/*
# throw
# Throwable
<http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html>
Only throwable objects may be thrown and declared thrown with `throws`.
The only stdlib implementing classes are Error and Exception
*/
{
// Special case explicit on JLS: `NullPointerException` is thrown.
try { throw null; } catch (NullPointerException e) {}
}
/*
# throws
# Checked exceptions
# Unchecked exceptions
<http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.6>
Every method whose interior may throw an exception must declare it,
unless they are descendants of:
- `Error`
- `RuntimeException`
which are therefore language-magic.
The following hierarchy exists:
Object
|
Throwable
|
+--Error
|
+--Exception
|
+--Runtime exception
|
+--Many others
Exceptions which require the `throws` clause, are called *checked*.
The others are called unchecked.
*/
{
// No need for `throws` because `Error` or `RuntimeError`.
try { throwError(); } catch (Throwable e) {}
try { throwRuntimeException(); } catch (Throwable e) {}
// ERROR: you must declare or compile error otherwise.
//try { throwNoSuchFieldExceptionFail(); } catch (Throwable e) {}
try { throwNoSuchFieldException(); } catch (Throwable e) {}
// Declaring throws with a base class also works.
// So declaring throws Throwable declares all.
try { throwNoSuchFieldExceptionBase(); } catch (Throwable e) {}
// ERROR: But you cannot declare throws Object because it is not Throwable.
//try { throwNoSuchFieldExceptionObject(); } catch (Throwable e) {}
// But you can declare an exception that is not thrown.
// TODO why?
assert declaresExceptionNotThrown() == 1;
}
/*
# finally
*/
{
/*
Can be used without catch.
All exceptions will blow up, but the finally will always get run.
*/
{
try {
} finally {
}
}
/*
# finally and return
Finally executes even after a return in the try.
But if you do return or break in the finally,
then the previous return is ignored!
*/
{
// TODO get working
assert finallyAndReturn() == -1;
}
}
/*
# OutOfMemoryError
Memory allowed by the JVM run out.
The maximum can be configured with command line options.
http://docs.oracle.com/javase/7/docs/api/java/lang/OutOfMemoryError.html
*/
/*
# IllegalArgumentException
Inherits from `RuntimeException`.
Not intended to be declared, since they indicate programming errors
that generally cannot be dealt with:
http://stackoverflow.com/questions/5304098/should-i-put-throws-illegalargumentexception-at-the-function
*/
}
private static class CloseableClass implements Closeable {
public static int i;
@Override
public void close() {
CloseableClass.i += 1;
}
}
private static void throwError() {
throw new Error();
}
private static void throwRuntimeException() {
throw new RuntimeException();
}
//private static void throwNoSuchFieldExceptionFail() {
//throw new NoSuchFieldException();
//}
private static void throwNoSuchFieldException() throws NoSuchFieldException {
throw new NoSuchFieldException();
}
private static void throwNoSuchFieldExceptionBase() throws Throwable {
throw new NoSuchFieldException();
}
// ERROR: incompatible types.
//private static void throwNoSuchFieldExceptionObject() throws Object {
//throw new NoSuchFieldException();
//}
private static int declaresExceptionNotThrown() throws Exception {
return 1;
}
private static int finallyAndReturn() {
label:
try {
return 1;
} finally {
break label;
}
return -1;
}
}