1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.backsource.utils.lang;
19
20 import java.io.PrintStream;
21 import java.io.PrintWriter;
22 /***
23 * <p>A nestable exception wich print the nested exception when printStackTrace is invoked.
24 *
25 * <p>The basis for all exceptions in timutils.
26 *
27 * @author Peter Antman
28 * @version $Revision: 1.1.1.1 $
29 */
30
31 public class NestableException extends Exception {
32 private Exception detail = null;
33 public NestableException() {
34 super();
35 }
36
37 public NestableException(Exception ex) {
38 super(ex.toString());
39 detail = ex;
40 }
41
42 public NestableException(String msg) {
43 super(msg);
44 }
45
46 public NestableException(String msg, Exception ex) {
47 super(msg);
48 detail = ex;
49 }
50
51 public String toString() {
52 String ret = super.toString();
53 ret = ret + (detail != null ? "\n\tWraps: " + detail.toString(): "");
54 return ret;
55 }
56
57 public void printStackTrace() {
58 super.printStackTrace();
59 if(detail != null)
60 detail.printStackTrace();
61 }
62
63 public void printStackTrace(PrintStream ps) {
64 super.printStackTrace(ps);
65 if(detail != null)
66 detail.printStackTrace(ps);
67 }
68
69 public void printStackTrace(PrintWriter writer) {
70 super.printStackTrace(writer);
71 if(detail != null)
72 detail.printStackTrace(writer);
73 }
74
75
76
77 public static void main(String args[]) {
78 try {
79 try {
80 Object o = new Integer(1);
81 String s = (String)o;
82 }catch(Exception ex) {
83 throw new NestableException("An exceptionoccured", ex);
84 }
85 }catch(NestableException ex) {
86 System.out.println(ex);
87 ex.printStackTrace();
88 }
89 try {
90 Runnable r = new Runnable() {
91 public void run() {
92 try {
93 try {
94 String st = null;
95 byte[] b = st.getBytes();
96 }catch(Exception ex) {
97 throw new NestableException("Err", ex);
98 }
99
100
101 }catch(Exception ex) {
102 ex.printStackTrace();
103 }
104 }
105 };
106 Thread t = new Thread(r);
107 t.start();
108
109 }catch(Exception ex) {
110 System.out.println(ex);
111 ex.printStackTrace();
112 }
113
114 }
115 }
116
117