KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbcp > SQLNestedException


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.commons.dbcp;
18
19 import java.io.PrintStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.sql.DriverManager JavaDoc;
23 import java.sql.SQLException JavaDoc;
24
25 /**
26  * A SQLException subclass containing another Throwable
27  *
28  * @author Dirk Verbeeck
29  * @version $Revision: 1.8 $ $Date: 2004/02/28 12:18:17 $
30  */

31 public class SQLNestedException extends SQLException JavaDoc {
32
33     /* Throwable.getCause detection as found in commons-lang */
34     private static final Method JavaDoc THROWABLE_CAUSE_METHOD;
35     static {
36         Method JavaDoc getCauseMethod;
37         try {
38             getCauseMethod = Throwable JavaDoc.class.getMethod("getCause", null);
39         } catch (Exception JavaDoc e) {
40             getCauseMethod = null;
41         }
42         THROWABLE_CAUSE_METHOD = getCauseMethod;
43     }
44     
45     private static boolean hasThrowableCauseMethod() {
46         return THROWABLE_CAUSE_METHOD != null;
47     }
48
49     /**
50      * Holds the reference to the exception or error that caused
51      * this exception to be thrown.
52      */

53     private Throwable JavaDoc cause = null;
54
55     /**
56      * Constructs a new <code>SQLNestedException</code> with specified
57      * detail message and nested <code>Throwable</code>.
58      *
59      * @param msg the error message
60      * @param cause the exception or error that caused this exception to be
61      * thrown
62      */

63     public SQLNestedException(String JavaDoc msg, Throwable JavaDoc cause) {
64         super(msg);
65         this.cause = cause;
66         if ((cause != null) && (DriverManager.getLogWriter() != null)) {
67             DriverManager.getLogWriter().print("Caused by: ");
68             cause.printStackTrace(DriverManager.getLogWriter());
69         }
70     }
71     
72     public Throwable JavaDoc getCause() {
73         return this.cause;
74     }
75
76     public void printStackTrace(PrintStream JavaDoc s) {
77         super.printStackTrace(s);
78         if ((cause != null) && !hasThrowableCauseMethod()) {
79             s.print("Caused by: ");
80             this.cause.printStackTrace(s);
81         }
82     }
83
84     public void printStackTrace(PrintWriter JavaDoc s) {
85         super.printStackTrace(s);
86         if ((cause != null) && !hasThrowableCauseMethod()) {
87             s.print("Caused by: ");
88             this.cause.printStackTrace(s);
89         }
90     }
91 }
92
Popular Tags