1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }