View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpException.java,v 1.18 2004/05/13 04:03:24 mbecke Exp $
3    * $Revision: 1.18 $
4    * $Date: 2004/05/13 04:03:24 $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
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          // If we're running on JDK 1.4 or later, tell Throwable what the cause was
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              // The setCause method must not be available
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             // JDK 1.4 has a nice printStackTrace method that prints the cause's stack
120             // trace too and prunes out duplicate stack frames.  Call it if possible,
121             // which is determined by checking whether JDK 1.4's getStackTrace method is present 
122             Class[] paramsClasses = new Class[] {  };
123             this.getClass().getMethod("getStackTrace", paramsClasses);
124             super.printStackTrace(s);
125         } catch (Exception ex) {
126             // If that didn't work, print it out ourselves
127             // First print this exception's stack trace.
128             super.printStackTrace(s);
129             if (cause != null) {
130                 // Print out the exception that caused this one.
131                 // This will recurse if the cause is another HttpException.
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             // JDK 1.4 has a nice printStackTrace method that prints the cause's stack
149             // trace too and prunes out duplicate stack frames.  Call it if possible,
150             // which is determined by checking whether JDK 1.4's getStackTrace method is present 
151             Class[] paramsClasses = new Class[] {  };
152             this.getClass().getMethod("getStackTrace", paramsClasses);
153             super.printStackTrace(s);
154         } catch (Exception ex) {
155             // If that didn't work, print it out ourselves
156             // First print this exception's stack trace.
157             super.printStackTrace(s);
158             if (cause != null) {
159                 // Print out the exception that caused this one.
160                 // This will recurse if the cause is another HttpException.
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 }