1 package org.jaxen;
2
3 import java.io.PrintStream;
4 import java.io.PrintWriter;
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 * This class exists to wrap Jaxen exceptions that otherwise wouldn't be propagated
69 * up through the axis iterators.
70 */
71 public class JaxenRuntimeException extends RuntimeException
72 {
73 private Throwable cause;
74 private boolean causeSet = false;
75
76 /***
77 * Create a new JaxenRuntimeException.
78 *
79 * @param cause the nested exception that's wrapped
80 * inside this exception
81 */
82 public JaxenRuntimeException(Throwable cause)
83 {
84 super(cause.getMessage());
85 initCause(cause);
86 }
87
88 /***
89 * Create a new JaxenRuntimeException.
90 *
91 * @param message the detail message
92 */
93 public JaxenRuntimeException(String message) {
94 super(message);
95 }
96
97 /***
98 * Returns the exception that caused this exception.
99 * This is necessary to implement Java 1.4 chained exception
100 * functionality in a Java 1.3-compatible way.
101 *
102 * @return the exception that caused this exception
103 */
104 public Throwable getCause() {
105 return cause;
106 }
107
108
109 /***
110 * Sets the exception that caused this exception.
111 * This is necessary to implement Java 1.4 chained exception
112 * functionality in a Java 1.3-compatible way.
113 *
114 * @param cause the exception wrapped in this runtime exception
115 *
116 * @return this exception
117 */
118 public Throwable initCause(Throwable cause) {
119 if (causeSet) throw new IllegalStateException("Cause cannot be reset");
120 if (cause == this) throw new IllegalArgumentException("Exception cannot be its own cause");
121 causeSet = true;
122 this.cause = cause;
123 return this;
124 }
125
126 /*** Print this exception's stack trace, followed by the
127 * source exception's trace, if any.
128 *
129 * @param s the stream on which to print the stack trace
130 */
131 public void printStackTrace ( PrintStream s )
132 {
133 super.printStackTrace ( s );
134 if (JaxenException.javaVersion < 1.4 && getCause() != null) {
135 s.print( "Caused by: " );
136 getCause().printStackTrace( s );
137 }
138 }
139
140 /*** Print this exception's stack trace, followed by the
141 * source exception's stack trace, if any.
142 *
143 * @param s the writer on which to print the stack trace
144 */
145 public void printStackTrace ( PrintWriter s )
146 {
147 super.printStackTrace( s );
148 if (JaxenException.javaVersion < 1.4 && getCause() != null) {
149 s.print( "Caused by: " );
150 getCause().printStackTrace( s );
151 }
152 }
153
154 }