KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > xtags > util > NestedRuntimeException


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.xtags.util;
18
19 import java.io.PrintStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 /** A {@link RuntimeException} which is nested to preserve stack traces.
23   *
24   * This class allows the following code to be written to convert a regular
25   * Exception into a {@link RuntimeException} without losing the stack trace.
26   *
27   * <pre>
28   * try {
29   * ...
30   * } catch (Exception e) {
31   * throw new RuntimeException(e);
32   * }
33   * </pre>
34   *
35   * @author James Strachan
36   */

37
38 public class NestedRuntimeException extends RuntimeException JavaDoc {
39     
40     private Exception JavaDoc nestedException;
41
42     //-------------------------------------------------------------------------
43
public NestedRuntimeException( Exception JavaDoc nestedException ) {
44         super( nestedException.getMessage() );
45         this.nestedException = nestedException;
46     }
47
48     public NestedRuntimeException( String JavaDoc message, Exception JavaDoc nestedException ) {
49         super( message );
50         this.nestedException = nestedException;
51     }
52
53     public void printStackTrace( PrintStream JavaDoc s ) {
54         nestedException.printStackTrace( s );
55     }
56     
57     public void printStackTrace( PrintWriter JavaDoc w ) {
58         nestedException.printStackTrace( w );
59     }
60     
61     public void printStackTrace() {
62         nestedException.printStackTrace();
63     }
64     
65     public Throwable JavaDoc fillInStackTrace() {
66         if ( nestedException == null ) {
67             return super.fillInStackTrace();
68         } else {
69             return nestedException.fillInStackTrace();
70         }
71     }
72     
73     // Properties
74
//-------------------------------------------------------------------------
75
public Exception JavaDoc getNestedException() {
76         return nestedException;
77     }
78     
79 }
80
Popular Tags