View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jelly.util;
18  
19  import java.io.PrintStream;
20  import java.io.PrintWriter;
21  
22  /***
23   * A {@link RuntimeException} which is nested to preserve stack traces.
24   *
25   * This class allows the following code to be written to convert a regular
26   * Exception into a {@link RuntimeException} without losing the stack trace.
27   *
28   * <pre>
29   *    try {
30   *        ...
31   *    } catch (Exception e) {
32   *        throw new RuntimeException(e);
33   *    }
34   * </pre>
35   *
36   * @author James Strachan
37   * @version $Revision: 155420 $
38   */
39  
40  public class NestedRuntimeException extends RuntimeException {
41  
42      /***
43       * Holds the reference to the exception or error that caused
44       * this exception to be thrown.
45       */
46      private Throwable cause = null;
47  
48      /***
49       * Constructs a new <code>NestedRuntimeException</code> with specified
50       * nested <code>Throwable</code>.
51       *
52       * @param cause the exception or error that caused this exception to be
53       * thrown
54       */
55      public NestedRuntimeException(Throwable cause) {
56          super(cause.getMessage());
57          this.cause = cause;
58      }
59  
60      /***
61       * Constructs a new <code>NestedRuntimeException</code> with specified
62       * detail message and nested <code>Throwable</code>.
63       *
64       * @param msg    the error message
65       * @param cause  the exception or error that caused this exception to be
66       * thrown
67       */
68      public NestedRuntimeException(String msg, Throwable cause) {
69          super(msg);
70          this.cause = cause;
71      }
72  
73      public Throwable getCause() {
74          return cause;
75      }
76  
77      public void printStackTrace() {
78          cause.printStackTrace();
79      }
80  
81      public void printStackTrace(PrintStream out) {
82          cause.printStackTrace(out);
83      }
84  
85      public void printStackTrace(PrintWriter out) {
86          cause.printStackTrace(out);
87      }
88  
89  }