1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.portals.graffito.jcr.exception;
17
18
19 import java.io.PrintStream;
20 import java.io.PrintWriter;
21
22 /***
23 * Nestable runtime exception. Able to wrap a cause exception in JDK previous to 1.4
24 *
25 * @author Spring framework
26 */
27 public class NestableRuntimeException extends RuntimeException {
28
29 /*** Use serialVersionUID for interoperability. */
30 private final static long serialVersionUID = -1939051127461985443L;
31
32 /*** Root cause of this nested exception */
33 private Throwable cause;
34
35 /***
36 * No-arg constructor used by markup exceptions.
37 */
38 protected NestableRuntimeException() {
39 }
40
41 /***
42 * Construct a <code>NestableRuntimeException</code> with the specified detail message.
43 * @param msg the detail message
44 */
45 public NestableRuntimeException(String msg) {
46 super(msg);
47 }
48
49 /***
50 * Construct a <code>NestableRuntimeException</code> with the specified detail message
51 * and nested exception.
52 * @param msg the detail message
53 * @param ex the nested exception
54 */
55 public NestableRuntimeException(String msg, Throwable ex) {
56 super(msg);
57 this.cause = ex;
58 }
59
60 /***
61 * Construct a <code>NestableRuntimeException</code> with the specified
62 * nested exception.
63 *
64 * @param ex the nested exception
65 */
66 public NestableRuntimeException(Throwable ex) {
67 this.cause = ex;
68 }
69
70 /***
71 * Return the nested cause, or <code>null</code> if none.
72 */
73 public Throwable getCause() {
74
75
76
77
78 return ((this.cause == this) ? null : this.cause);
79 }
80
81 /***
82 * Return the detail message, including the message from the nested exception
83 * if there is one.
84 */
85 public String getMessage() {
86 if (getCause() == null) {
87 return super.getMessage();
88 } else {
89 return super.getMessage()
90 + "; nested exception is "
91 + getCause().getClass().getName()
92 + ": " + getCause().getMessage();
93 }
94 }
95
96 /***
97 * Print the composite message and the embedded stack trace to the specified stream.
98 * @param ps the print stream
99 */
100 public void printStackTrace(PrintStream ps) {
101 if (getCause() == null) {
102 super.printStackTrace(ps);
103 } else {
104 ps.println(this);
105 getCause().printStackTrace(ps);
106 }
107 }
108
109 /***
110 * Print the composite message and the embedded stack trace to the specified writer.
111 * @param pw the print writer
112 */
113 public void printStackTrace(PrintWriter pw) {
114 if (getCause() == null) {
115 super.printStackTrace(pw);
116 } else {
117 pw.println(this);
118 getCause().printStackTrace(pw);
119 }
120 }
121 }