1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.dbcp;
19
20 import java.io.PrintStream;
21 import java.io.PrintWriter;
22 import java.lang.reflect.Method;
23 import java.sql.DriverManager;
24 import java.sql.SQLException;
25
26 /**
27 * A SQLException subclass containing another Throwable
28 *
29 * @author Dirk Verbeeck
30 * @version $Revision: 559119 $ $Date: 2007-07-24 13:24:03 -0400 (Tue, 24 Jul 2007) $
31 * @deprecated Use '(SQLException) new SQLException(msg).initCause(e)' instead; this class will be removed in DBCP 1.4
32 */
33 public class SQLNestedException extends SQLException {
34
35 /* Throwable.getCause detection as found in commons-lang */
36 private static final Method THROWABLE_CAUSE_METHOD;
37 static {
38 Method getCauseMethod;
39 try {
40 getCauseMethod = Throwable.class.getMethod("getCause", (Class[]) null);
41 } catch (Exception e) {
42 getCauseMethod = null;
43 }
44 THROWABLE_CAUSE_METHOD = getCauseMethod;
45 }
46
47 private static boolean hasThrowableCauseMethod() {
48 return THROWABLE_CAUSE_METHOD != null;
49 }
50
51 /**
52 * Holds the reference to the exception or error that caused
53 * this exception to be thrown.
54 */
55 private Throwable cause = null;
56
57 /**
58 * Constructs a new <code>SQLNestedException</code> with specified
59 * detail message and nested <code>Throwable</code>.
60 *
61 * @param msg the error message
62 * @param cause the exception or error that caused this exception to be
63 * thrown
64 */
65 public SQLNestedException(String msg, Throwable cause) {
66 super(msg);
67 this.cause = cause;
68 if ((cause != null) && (DriverManager.getLogWriter() != null)) {
69 DriverManager.getLogWriter().print("Caused by: ");
70 cause.printStackTrace(DriverManager.getLogWriter());
71 }
72 }
73
74 public Throwable getCause() {
75 return this.cause;
76 }
77
78 public void printStackTrace(PrintStream s) {
79 super.printStackTrace(s);
80 if ((cause != null) && !hasThrowableCauseMethod()) {
81 s.print("Caused by: ");
82 this.cause.printStackTrace(s);
83 }
84 }
85
86 public void printStackTrace(PrintWriter s) {
87 super.printStackTrace(s);
88 if ((cause != null) && !hasThrowableCauseMethod()) {
89 s.print("Caused by: ");
90 this.cause.printStackTrace(s);
91 }
92 }
93 }