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