KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > NestedIOException


1 /*
2  * Copyright 2002-2006 the original author or authors.
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.springframework.core;
18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintStream JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22
23 /**
24  * Subclass of IOException that properly handles a root cause,
25  * exposing the root cause just like NestedChecked/RuntimeException does.
26  *
27  * <p>The similarity between this class and the NestedChecked/RuntimeException
28  * class is unavoidable, as this class needs to derive from IOException
29  * and cannot derive from NestedCheckedException.
30  *
31  * @author Juergen Hoeller
32  * @since 2.0
33  * @see #getMessage
34  * @see #printStackTrace
35  * @see org.springframework.core.NestedCheckedException
36  * @see org.springframework.core.NestedRuntimeException
37  */

38 public class NestedIOException extends IOException JavaDoc {
39
40     /** Root cause of this nested exception */
41     private Throwable JavaDoc cause;
42
43
44     /**
45      * Construct a <code>NestedServletException</code> with the specified detail message.
46      * @param msg the detail message
47      */

48     public NestedIOException(String JavaDoc msg) {
49         super(msg);
50     }
51
52     /**
53      * Construct a <code>NestedServletException</code> with the specified detail message
54      * and nested exception.
55      * @param msg the detail message
56      * @param cause the nested exception
57      */

58     public NestedIOException(String JavaDoc msg, Throwable JavaDoc cause) {
59         super(msg);
60         this.cause = cause;
61     }
62
63
64     /**
65      * Return the nested cause, or <code>null</code> if none.
66      */

67     public Throwable JavaDoc getCause() {
68         // Even if you cannot set the cause of this exception other than through
69
// the constructor, we check for the cause being "this" here, as the cause
70
// could still be set to "this" via reflection: for example, by a remoting
71
// deserializer like Hessian's.
72
return (this.cause == this ? null : this.cause);
73     }
74
75     /**
76      * Return the detail message, including the message from the nested exception
77      * if there is one.
78      */

79     public String JavaDoc getMessage() {
80         return NestedExceptionUtils.buildMessage(super.getMessage(), getCause());
81     }
82
83     /**
84      * Print the composite message and the embedded stack trace to the specified stream.
85      * @param ps the print stream
86      */

87     public void printStackTrace(PrintStream JavaDoc ps) {
88         if (getCause() == null) {
89             super.printStackTrace(ps);
90         }
91         else {
92             ps.println(this);
93             ps.print("Caused by: ");
94             getCause().printStackTrace(ps);
95         }
96     }
97
98     /**
99      * Print the composite message and the embedded stack trace to the specified print writer.
100      * @param pw the print writer
101      */

102     public void printStackTrace(PrintWriter JavaDoc pw) {
103         if (getCause() == null) {
104             super.printStackTrace(pw);
105         }
106         else {
107             pw.println(this);
108             pw.print("Caused by: ");
109             getCause().printStackTrace(pw);
110         }
111     }
112
113 }
114
Popular Tags