01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
31: */
32: public class SQLNestedException extends SQLException {
33:
34: /* Throwable.getCause detection as found in commons-lang */
35: private static final Method THROWABLE_CAUSE_METHOD;
36: static {
37: Method getCauseMethod;
38: try {
39: getCauseMethod = Throwable.class.getMethod("getCause",
40: (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: }
|