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 package org.apache.commons.httpclient;
31
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import java.io.PrintWriter;
35 import java.lang.reflect.Method;
36
37 /***
38 * Signals that an HTTP or HttpClient exception has occurred.
39 *
40 * @author Laura Werner
41 *
42 * @version $Revision: 1.18 $ $Date: 2004/05/13 04:03:24 $
43 */
44 public class HttpException extends IOException {
45
46 /***
47 * Creates a new HttpException with a <tt>null</tt> detail message.
48 */
49 public HttpException() {
50 super();
51 this.cause = null;
52 }
53
54 /***
55 * Creates a new HttpException with the specified detail message.
56 *
57 * @param message the exception detail message
58 */
59 public HttpException(String message) {
60 super(message);
61 this.cause = null;
62 }
63
64 /***
65 * Creates a new HttpException with the specified detail message and cause.
66 *
67 * @param message the exception detail message
68 * @param cause the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
69 * if the cause is unavailable, unknown, or not a <tt>Throwable</tt>
70 *
71 * @since 3.0
72 */
73 public HttpException(String message, Throwable cause) {
74 super(message);
75 this.cause = cause;
76
77
78 try {
79 Class[] paramsClasses = new Class[] { Throwable.class };
80 Method initCause = Throwable.class.getMethod("initCause", paramsClasses);
81 initCause.invoke(this, new Object[] { cause });
82 } catch (Exception e) {
83
84 }
85 }
86
87 /***
88 * Return the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
89 * if the cause is unavailable, unknown, or not a <tt>Throwable</tt>.
90 *
91 * @return the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
92 * if the cause is unavailable, unknown, or not a <tt>Throwable</tt>
93 *
94 * @since 3.0
95 */
96 public Throwable getCause() {
97 return cause;
98 }
99
100 /***
101 * Print this HttpException and its stack trace to the standard error stream.
102 *
103 * @since 3.0
104 */
105 public void printStackTrace() {
106 printStackTrace(System.err);
107 }
108
109 /***
110 * Print this HttpException and its stack trace to the specified print stream.
111 *
112 * @param s the <tt>PrintStream</tt> to which the exception and its stack trace
113 * should be written
114 *
115 * @since 3.0
116 */
117 public void printStackTrace(PrintStream s) {
118 try {
119
120
121
122 Class[] paramsClasses = new Class[] { };
123 this.getClass().getMethod("getStackTrace", paramsClasses);
124 super.printStackTrace(s);
125 } catch (Exception ex) {
126
127
128 super.printStackTrace(s);
129 if (cause != null) {
130
131
132 s.print("Caused by: ");
133 cause.printStackTrace(s);
134 }
135 }
136 }
137
138 /***
139 * Print this HttpException and its stack trace to the specified print writer.
140 *
141 * @param s the <tt>PrintWriter</tt> to which the exception and its stack trace
142 * should be written
143 *
144 * @since 3.0
145 */
146 public void printStackTrace(PrintWriter s) {
147 try {
148
149
150
151 Class[] paramsClasses = new Class[] { };
152 this.getClass().getMethod("getStackTrace", paramsClasses);
153 super.printStackTrace(s);
154 } catch (Exception ex) {
155
156
157 super.printStackTrace(s);
158 if (cause != null) {
159
160
161 s.print("Caused by: ");
162 cause.printStackTrace(s);
163 }
164 }
165 }
166
167 /*** The original Throwable representing the cause of this error */
168 private final Throwable cause;
169 }