01: /* ****************************************************************************
02: * ChainedException.java
03: * ****************************************************************************/
04:
05: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
06: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
07: * Use is subject to license terms. *
08: * J_LZ_COPYRIGHT_END *********************************************************/
09:
10: package org.openlaszlo.utils;
11:
12: import java.io.PrintStream;
13:
14: /** A checked chained exception. */
15: public class ChainedException extends RuntimeException {
16: Throwable cause = null;
17:
18: /** Constructs an instance.
19: *
20: * @param message a string
21: */
22: public ChainedException(String message) {
23: super (message);
24: }
25:
26: /** Constructs a new runtime exception with the specified detail
27: * message and cause.
28: *
29: * @param message the detail message (which is saved for later retrieval by
30: * the Throwable.getMessage() method).
31: * @param cause the cause (which is saved for later retrieval by the
32: * Throwable.getCause() method). (A null value is permitted, and indicates
33: * that the cause is nonexistent or unknown.)
34: */
35: public ChainedException(String message, Throwable cause) {
36: super (cause.getClass().getName() + ": " + message);
37: this .cause = cause;
38: }
39:
40: /** Constructs a new runtime exception with the specified cause, which
41: * typically contains the class and detail message of cause.
42: *
43: * @param cause the cause, which is saved for later retrieval by the
44: * Throwable.getCause() method. A null value is permitted, and indicates
45: * that the cause is nonexistent or unknown.
46: */
47: public ChainedException(Throwable cause) {
48: super (cause.getClass().getName() + ": " + cause.getMessage());
49: this .cause = cause;
50: }
51:
52: /** Returns the cause of this throwable or null if the cause is nonexistent
53: * or unknown. (The cause is the throwable that caused this throwable to get
54: * thrown.)
55: *
56: * @return the cause of this throwable or null if the cause is nonexistent
57: * or unknown.
58: */
59: public Throwable getCause() {
60: return this .cause;
61: }
62:
63: /** Prints this throwable and its backtrace to the specified print stream.
64: *
65: * @param s PrintStream to use for output.
66: */
67: public void printStackTrace(PrintStream s) {
68: super .printStackTrace(s);
69: if (cause != null) {
70: s.print("Caused by: ");
71: cause.printStackTrace(s);
72: }
73: }
74:
75: /** Prints this throwable and its backtrace to the specified print stream.
76: *
77: * @param s PrintWriter to use for output.
78: */
79: public void printStackTrace(java.io.PrintWriter pw) {
80: super .printStackTrace(pw);
81: if (cause != null) {
82: pw.println("Caused by:");
83: cause.printStackTrace(pw);
84: }
85: }
86: }
|