1 16 17 package org.apache.commons.dbcp; 18 19 import java.io.PrintStream ; 20 import java.io.PrintWriter ; 21 import java.lang.reflect.Method ; 22 import java.sql.DriverManager ; 23 import java.sql.SQLException ; 24 25 31 public class SQLNestedException extends SQLException { 32 33 34 private static final Method THROWABLE_CAUSE_METHOD; 35 static { 36 Method getCauseMethod; 37 try { 38 getCauseMethod = Throwable .class.getMethod("getCause", null); 39 } catch (Exception 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 53 private Throwable cause = null; 54 55 63 public SQLNestedException(String msg, Throwable 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 getCause() { 73 return this.cause; 74 } 75 76 public void printStackTrace(PrintStream 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 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 |