1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang.exception;
  55. import java.io.PrintStream;
  56. import java.io.PrintWriter;
  57. /**
  58. * The base class of all exceptions which can contain other exceptions.
  59. *
  60. * It is intended to ease the debugging by carrying on the information
  61. * about the exception which was caught and provoked throwing the
  62. * current exception. Catching and rethrowing may occur multiple
  63. * times, and provided that all exceptions except the first one
  64. * are descendands of <code>NestedException</code>, when the
  65. * exception is finally printed out using any of the <code>
  66. * printStackTrace()</code> methods, the stacktrace will contain
  67. * the information about all exceptions thrown and caught on
  68. * the way.
  69. * <p> Running the following program
  70. * <p><blockquote><pre>
  71. * 1 import org.apache.commons.lang.exception.NestableException;
  72. * 2
  73. * 3 public class Test {
  74. * 4 public static void main( String[] args ) {
  75. * 5 try {
  76. * 6 a();
  77. * 7 } catch(Exception e) {
  78. * 8 e.printStackTrace();
  79. * 9 }
  80. * 10 }
  81. * 11
  82. * 12 public static void a() throws Exception {
  83. * 13 try {
  84. * 14 b();
  85. * 15 } catch(Exception e) {
  86. * 16 throw new NestableException("foo", e);
  87. * 17 }
  88. * 18 }
  89. * 19
  90. * 20 public static void b() throws Exception {
  91. * 21 try {
  92. * 22 c();
  93. * 23 } catch(Exception e) {
  94. * 24 throw new NestableException("bar", e);
  95. * 25 }
  96. * 26 }
  97. * 27
  98. * 28 public static void c() throws Exception {
  99. * 29 throw new Exception("baz");
  100. * 30 }
  101. * 31 }
  102. * </pre></blockquote>
  103. * <p>Yields the following stacktrace:
  104. * <p><blockquote><pre>
  105. * org.apache.commons.lang.exception.NestableException: foo
  106. * at Test.a(Test.java:16)
  107. * at Test.main(Test.java:6)
  108. * Caused by: org.apache.commons.lang.exception.NestableException: bar
  109. * at Test.b(Test.java:24)
  110. * at Test.a(Test.java:14)
  111. * ... 1 more
  112. * Caused by: java.lang.Exception: baz
  113. * at Test.c(Test.java:29)
  114. * at Test.b(Test.java:22)
  115. * ... 2 more
  116. * </pre></blockquote><br>
  117. *
  118. * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
  119. * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
  120. * @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
  121. * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
  122. * @since 1.0
  123. * @version $Id: NestableException.java,v 1.9 2003/08/18 02:22:24 bayard Exp $
  124. */
  125. public class NestableException extends Exception implements Nestable {
  126. /**
  127. * The helper instance which contains much of the code which we
  128. * delegate to.
  129. */
  130. protected NestableDelegate delegate = new NestableDelegate(this);
  131. /**
  132. * Holds the reference to the exception or error that caused
  133. * this exception to be thrown.
  134. */
  135. private Throwable cause = null;
  136. /**
  137. * Constructs a new <code>NestableException</code> without specified
  138. * detail message.
  139. */
  140. public NestableException() {
  141. super();
  142. }
  143. /**
  144. * Constructs a new <code>NestableException</code> with specified
  145. * detail message.
  146. *
  147. * @param msg The error message.
  148. */
  149. public NestableException(String msg) {
  150. super(msg);
  151. }
  152. /**
  153. * Constructs a new <code>NestableException</code> with specified
  154. * nested <code>Throwable</code>.
  155. *
  156. * @param cause the exception or error that caused this exception to be
  157. * thrown
  158. */
  159. public NestableException(Throwable cause) {
  160. super();
  161. this.cause = cause;
  162. }
  163. /**
  164. * Constructs a new <code>NestableException</code> with specified
  165. * detail message and nested <code>Throwable</code>.
  166. *
  167. * @param msg the error message
  168. * @param cause the exception or error that caused this exception to be
  169. * thrown
  170. */
  171. public NestableException(String msg, Throwable cause) {
  172. super(msg);
  173. this.cause = cause;
  174. }
  175. public Throwable getCause() {
  176. return cause;
  177. }
  178. /**
  179. * Returns the detail message string of this throwable. If it was
  180. * created with a null message, returns the following:
  181. * (cause==null ? null : cause.toString()).
  182. */
  183. public String getMessage() {
  184. if (super.getMessage() != null) {
  185. return super.getMessage();
  186. } else if (cause != null) {
  187. return cause.toString();
  188. } else {
  189. return null;
  190. }
  191. }
  192. public String getMessage(int index) {
  193. if (index == 0) {
  194. return super.getMessage();
  195. } else {
  196. return delegate.getMessage(index);
  197. }
  198. }
  199. public String[] getMessages() {
  200. return delegate.getMessages();
  201. }
  202. public Throwable getThrowable(int index) {
  203. return delegate.getThrowable(index);
  204. }
  205. public int getThrowableCount() {
  206. return delegate.getThrowableCount();
  207. }
  208. public Throwable[] getThrowables() {
  209. return delegate.getThrowables();
  210. }
  211. public int indexOfThrowable(Class type) {
  212. return delegate.indexOfThrowable(type, 0);
  213. }
  214. public int indexOfThrowable(Class type, int fromIndex) {
  215. return delegate.indexOfThrowable(type, fromIndex);
  216. }
  217. public void printStackTrace() {
  218. delegate.printStackTrace();
  219. }
  220. public void printStackTrace(PrintStream out) {
  221. delegate.printStackTrace(out);
  222. }
  223. public void printStackTrace(PrintWriter out) {
  224. delegate.printStackTrace(out);
  225. }
  226. public final void printPartialStackTrace(PrintWriter out) {
  227. super.printStackTrace(out);
  228. }
  229. }