forked from cirosantilli/java-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExceptionCheat.java
37 lines (34 loc) · 999 Bytes
/
ExceptionCheat.java
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
/*
What happens when an exception blows up
and gets forwarded across a method chain.
Too long to assert.
*/
public class ExceptionCheat {
static void f5() { throw null; }
static void f4() {
try {
f5();
} finally {
/*
If you throw from either finally or catch, it stops propagating the current exception
and starts a new one.
From the catch, you can keep the stack trace via the exception argument,
but from finally, it's lost for good.
*/
throw null;
}
}
static void f3() { f4(); }
static void f2() throws Exception {
try {
f3();
} catch (Throwable e) {
// You must pass `e` here to pass the stack trace forward.
throw new Exception("Custom Message", e);
}
}
static void f1() throws Exception { f2(); }
public static void main(String[] args) throws Exception {
f1();
}
}