author | katleman |
Tue, 21 Jan 2014 18:17:07 -0800 | |
changeset 22414 | 796c8cc1bc35 |
parent 9513 | 1079ae7ada52 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* @test /nodynamiccopyright/ |
|
3 |
* @bug 4209652 4363318 |
|
4 |
* @summary Basic test for chained exceptions & Exception.getStackTrace(). |
|
5 |
* @author Josh Bloch |
|
6 |
*/ |
|
7 |
||
8 |
public class ChainedExceptions { |
|
9 |
public static void main(String args[]) { |
|
10 |
try { |
|
11 |
a(); |
|
12 |
} catch(HighLevelException e) { |
|
13 |
StackTraceElement[] highTrace = e.getStackTrace(); |
|
14 |
int depthTrim = highTrace.length - 2; |
|
15 |
||
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
16 |
check(e, highTrace[0], "a", 48); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
17 |
check(e, highTrace[1], "main", 11); |
2 | 18 |
|
19 |
Throwable mid = e.getCause(); |
|
20 |
StackTraceElement[] midTrace = mid.getStackTrace(); |
|
21 |
if (midTrace.length - depthTrim != 4) |
|
22 |
throw new RuntimeException("Mid depth"); |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
23 |
check(mid, midTrace[0], "c", 58); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
24 |
check(mid, midTrace[1], "b", 52); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
25 |
check(mid, midTrace[2], "a", 46); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
26 |
check(mid, midTrace[3], "main", 11); |
2 | 27 |
|
28 |
Throwable low = mid.getCause(); |
|
29 |
StackTraceElement[] lowTrace = low.getStackTrace(); |
|
30 |
if (lowTrace.length - depthTrim != 6) |
|
31 |
throw new RuntimeException("Low depth"); |
|
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
32 |
check(low, lowTrace[0], "e", 65); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
33 |
check(low, lowTrace[1], "d", 62); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
34 |
check(low, lowTrace[2], "c", 56); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
35 |
check(low, lowTrace[3], "b", 52); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
36 |
check(low, lowTrace[4], "a", 46); |
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
37 |
check(low, lowTrace[5], "main", 11); |
2 | 38 |
|
39 |
if (low.getCause() != null) |
|
40 |
throw new RuntimeException("Low cause != null"); |
|
41 |
} |
|
42 |
} |
|
43 |
||
44 |
static void a() throws HighLevelException { |
|
45 |
try { |
|
46 |
b(); |
|
47 |
} catch(MidLevelException e) { |
|
48 |
throw new HighLevelException(e); |
|
49 |
} |
|
50 |
} |
|
51 |
static void b() throws MidLevelException { |
|
52 |
c(); |
|
53 |
} |
|
54 |
static void c() throws MidLevelException { |
|
55 |
try { |
|
56 |
d(); |
|
57 |
} catch(LowLevelException e) { |
|
58 |
throw new MidLevelException(e); |
|
59 |
} |
|
60 |
} |
|
61 |
static void d() throws LowLevelException { |
|
62 |
e(); |
|
63 |
} |
|
64 |
static void e() throws LowLevelException { |
|
65 |
throw new LowLevelException(); |
|
66 |
} |
|
67 |
||
68 |
private static final String OUR_CLASS = ChainedExceptions.class.getName(); |
|
69 |
private static final String OUR_FILE_NAME = "ChainedExceptions.java"; |
|
70 |
||
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
71 |
private static void check(Throwable t, StackTraceElement e, String methodName, int n) { |
2 | 72 |
if (!e.getClassName().equals(OUR_CLASS)) |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
73 |
throw new RuntimeException("Class: " + e, t); |
2 | 74 |
if (!e.getMethodName().equals(methodName)) |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
75 |
throw new RuntimeException("Method name: " + e, t); |
2 | 76 |
if (!e.getFileName().equals(OUR_FILE_NAME)) |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
77 |
throw new RuntimeException("File name: " + e, t); |
2 | 78 |
if (e.getLineNumber() != n) |
9513
1079ae7ada52
6998871: Support making the Throwable.stackTrace field immutable
darcy
parents:
2
diff
changeset
|
79 |
throw new RuntimeException("Line number: " + e, t); |
2 | 80 |
} |
81 |
} |
|
82 |
||
83 |
class HighLevelException extends Exception { |
|
84 |
HighLevelException(Throwable cause) { super(cause); } |
|
85 |
} |
|
86 |
||
87 |
class MidLevelException extends Exception { |
|
88 |
MidLevelException(Throwable cause) { super(cause); } |
|
89 |
} |
|
90 |
||
91 |
class LowLevelException extends Exception { |
|
92 |
} |