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 package org.jaxen;
64
65 import java.io.PrintStream;
66 import java.io.PrintWriter;
67
68
69 /***
70 * Generic Jaxen exception.
71 *
72 * <p> This is the root of all Jaxen exceptions. It may wrap other exceptions.
73 *
74 * @author <a href="mailto:bob@werken.com">bob mcwhirter</a>
75 */
76 public class JaxenException extends org.jaxen.saxpath.SAXPathException
77 {
78
79 static double javaVersion = 1.4;
80
81 static {
82 try {
83 String versionString = System.getProperty("java.version");
84 versionString = versionString.substring(0, 3);
85 javaVersion = Double.valueOf(versionString).doubleValue();
86 }
87 catch (Exception ex) {
88
89
90 }
91 }
92
93 /***
94 * Create an exception with a detail message.
95 *
96 * @param message the error message
97 */
98 public JaxenException( String message )
99 {
100 super( message );
101 }
102
103 /***
104 * Create an exception caused by another exception.
105 *
106 * @param rootCause the root cause of this exception
107 */
108 public JaxenException( Throwable rootCause )
109 {
110 super( rootCause );
111 }
112
113 /***
114 * Create a new JaxenException with the specified detail message
115 * and root cause.
116 *
117 * @param message the detail message
118 * @param nestedException the cause of this exception
119 */
120 public JaxenException(String message, Throwable nestedException) {
121 super( message, nestedException );
122 }
123
124 private Throwable cause;
125 private boolean causeSet = false;
126
127 /***
128 * Returns the exception that caused this exception.
129 * This is necessary to implement Java 1.4 chained exception
130 * functionality in a Java 1.3-compatible way.
131 *
132 * @return the exception that caused this exception
133 */
134 public Throwable getCause() {
135 return cause;
136 }
137
138
139 /***
140 * Sets the exception that caused this exception.
141 * This is necessary to implement Java 1.4 chained exception
142 * functionality in a Java 1.3-compatible way.
143 *
144 * @param cause the exception wrapped in this runtime exception
145 *
146 * @return this exception
147 */
148 public Throwable initCause(Throwable cause) {
149 if (causeSet) throw new IllegalStateException("Cause cannot be reset");
150 if (cause == this) throw new IllegalArgumentException("Exception cannot be its own cause");
151 causeSet = true;
152 this.cause = cause;
153 return this;
154 }
155
156 public void printStackTrace( PrintStream s ) {
157 super.printStackTrace( s );
158 if ( javaVersion < 1.4 && getCause() != null )
159 {
160 s.print( "Caused by: " );
161 getCause().printStackTrace( s );
162 }
163 }
164
165 public void printStackTrace( PrintWriter w ) {
166 super.printStackTrace( w );
167
168 if ( javaVersion < 1.4 && getCause() != null )
169 {
170 w.print( "Caused by: " );
171 getCause().printStackTrace( w );
172 }
173 }
174
175 }
176