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 runtime exceptions which can contain other
  59. * exceptions.
  60. *
  61. * @see org.apache.commons.lang.exception.NestableException
  62. * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
  63. * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
  64. * @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
  65. * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
  66. * @since 1.0
  67. * @version $Id: NestableRuntimeException.java,v 1.9 2003/08/18 02:22:24 bayard Exp $
  68. */
  69. public class NestableRuntimeException extends RuntimeException implements Nestable {
  70. /**
  71. * The helper instance which contains much of the code which we
  72. * delegate to.
  73. */
  74. protected NestableDelegate delegate = new NestableDelegate(this);
  75. /**
  76. * Holds the reference to the exception or error that caused
  77. * this exception to be thrown.
  78. */
  79. private Throwable cause = null;
  80. /**
  81. * Constructs a new <code>NestableRuntimeException</code> without specified
  82. * detail message.
  83. */
  84. public NestableRuntimeException() {
  85. super();
  86. }
  87. /**
  88. * Constructs a new <code>NestableRuntimeException</code> with specified
  89. * detail message.
  90. *
  91. * @param msg the error message
  92. */
  93. public NestableRuntimeException(String msg) {
  94. super(msg);
  95. }
  96. /**
  97. * Constructs a new <code>NestableRuntimeException</code> with specified
  98. * nested <code>Throwable</code>.
  99. *
  100. * @param cause the exception or error that caused this exception to be
  101. * thrown
  102. */
  103. public NestableRuntimeException(Throwable cause) {
  104. super();
  105. this.cause = cause;
  106. }
  107. /**
  108. * Constructs a new <code>NestableRuntimeException</code> with specified
  109. * detail message and nested <code>Throwable</code>.
  110. *
  111. * @param msg the error message
  112. * @param cause the exception or error that caused this exception to be
  113. * thrown
  114. */
  115. public NestableRuntimeException(String msg, Throwable cause) {
  116. super(msg);
  117. this.cause = cause;
  118. }
  119. public Throwable getCause() {
  120. return cause;
  121. }
  122. /**
  123. * Returns the detail message string of this throwable. If it was
  124. * created with a null message, returns the following:
  125. * (cause==null ? null : cause.toString()).
  126. */
  127. public String getMessage() {
  128. if (super.getMessage() != null) {
  129. return super.getMessage();
  130. } else if (cause != null) {
  131. return cause.toString();
  132. } else {
  133. return null;
  134. }
  135. }
  136. public String getMessage(int index) {
  137. if (index == 0) {
  138. return super.getMessage();
  139. } else {
  140. return delegate.getMessage(index);
  141. }
  142. }
  143. public String[] getMessages() {
  144. return delegate.getMessages();
  145. }
  146. public Throwable getThrowable(int index) {
  147. return delegate.getThrowable(index);
  148. }
  149. public int getThrowableCount() {
  150. return delegate.getThrowableCount();
  151. }
  152. public Throwable[] getThrowables() {
  153. return delegate.getThrowables();
  154. }
  155. public int indexOfThrowable(Class type) {
  156. return delegate.indexOfThrowable(type, 0);
  157. }
  158. public int indexOfThrowable(Class type, int fromIndex) {
  159. return delegate.indexOfThrowable(type, fromIndex);
  160. }
  161. public void printStackTrace() {
  162. delegate.printStackTrace();
  163. }
  164. public void printStackTrace(PrintStream out) {
  165. delegate.printStackTrace(out);
  166. }
  167. public void printStackTrace(PrintWriter out) {
  168. delegate.printStackTrace(out);
  169. }
  170. public final void printPartialStackTrace(PrintWriter out) {
  171. super.printStackTrace(out);
  172. }
  173. }