1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.scaffold.lang; |
18 | |
|
19 | |
import org.apache.commons.scaffold.text.ConvertUtils; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class ChainedException extends Exception { |
29 | |
|
30 | |
private static final String CAUSED_BY = "Caused by: "; |
31 | |
|
32 | |
private Throwable cause; |
33 | |
|
34 | |
public ChainedException() { |
35 | 0 | super(); |
36 | 0 | } |
37 | |
|
38 | |
public ChainedException(String message) { |
39 | 0 | super(message); |
40 | 0 | } |
41 | |
|
42 | |
public ChainedException(String message, Throwable cause) { |
43 | 0 | super(message); |
44 | 0 | this.cause = cause; |
45 | 0 | } |
46 | |
|
47 | |
public ChainedException(Throwable cause) { |
48 | 0 | super(); |
49 | 0 | this.cause = cause; |
50 | 0 | } |
51 | |
|
52 | |
public Throwable getCause() { |
53 | 0 | return this.cause; |
54 | |
} |
55 | |
|
56 | |
public boolean isCause() { |
57 | 0 | return (this.cause!=null); |
58 | |
} |
59 | |
|
60 | |
public String getCauseMessage() { |
61 | 0 | if (this.cause==null) |
62 | 0 | return null; |
63 | 0 | return this.cause.getMessage(); |
64 | |
} |
65 | |
|
66 | |
public void getMessage(StringBuffer sb) { |
67 | 0 | sb.append(super.getMessage()); |
68 | 0 | sb.append(ConvertUtils.LINE_FEED); |
69 | 0 | if (cause != null) { |
70 | 0 | sb.append(CAUSED_BY); |
71 | 0 | if (cause instanceof ChainedException) { |
72 | 0 | ChainedException chainedCause = (ChainedException) cause; |
73 | 0 | chainedCause.getMessage(sb); |
74 | 0 | } |
75 | |
else { |
76 | 0 | sb.append(cause.getMessage()); |
77 | |
} |
78 | |
} |
79 | 0 | } |
80 | |
|
81 | |
public void printStackTrace() { |
82 | 0 | super.printStackTrace(); |
83 | 0 | if (cause != null) { |
84 | 0 | System.err.println(CAUSED_BY); |
85 | 0 | cause.printStackTrace(); |
86 | |
} |
87 | 0 | } |
88 | |
|
89 | |
public void printStackTrace(java.io.PrintStream ps) { |
90 | 0 | super.printStackTrace(ps); |
91 | 0 | if (cause != null) { |
92 | 0 | ps.println(CAUSED_BY); |
93 | 0 | cause.printStackTrace(ps); |
94 | |
} |
95 | 0 | } |
96 | |
|
97 | |
public void printStackTrace(java.io.PrintWriter pw) { |
98 | 0 | super.printStackTrace(pw); |
99 | 0 | if (cause != null) { |
100 | 0 | pw.println(CAUSED_BY); |
101 | 0 | cause.printStackTrace(pw); |
102 | |
} |
103 | 0 | } |
104 | |
} |
105 | |
|