View Javadoc

1   /*
2    * Copyright (c) 2000 Peter Antman Tim <peter.antman@tim.se>
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2 of the License, or (at your option) any later version
8    * 
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   * 
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 } // NestableException
116 
117