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