KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > crazybob > util > NestedException


1 package org.crazybob.util;
2
3 import java.io.*;
4
5 /**
6  * Turns a nested exception into a runtime exception. Shields client
7  * from vendor-specific implementations. Use for exceptions that you don't
8  * know how to or shouldn't handle in your code. Maintains original
9  * exception's message and stack trace.
10  *
11  * @author Bob Lee (crazybob@crazybob.org)
12  */

13 public class NestedException extends RuntimeException JavaDoc {
14
15     /** Wrap another exeception in a RuntimeException. */
16     public static RuntimeException JavaDoc wrap(Throwable JavaDoc t) {
17         if (t instanceof RuntimeException JavaDoc) return (RuntimeException JavaDoc) t;
18         return new NestedException(t);
19     }
20
21     private String JavaDoc message;
22     private String JavaDoc stackTrace;
23
24     /** Wraps another exeception. */
25     private NestedException(Throwable JavaDoc t) {
26         super();
27         this.message = t.getMessage();
28         StringWriter out = new StringWriter();
29         t.printStackTrace(new PrintWriter(out));
30         this.stackTrace = out.toString();
31     }
32
33     public String JavaDoc toString() {
34         return this.getMessage();
35     }
36
37     public String JavaDoc getMessage() {
38         return this.message;
39     }
40
41     public void printStackTrace() {
42         System.err.print(this.stackTrace);
43     }
44
45     public void printStackTrace(PrintStream out) {
46         printStackTrace(new PrintWriter(out));
47     }
48
49     public void printStackTrace(PrintWriter out) {
50         out.print(this.stackTrace);
51     }
52
53 }
54
Popular Tags