You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by di...@apache.org on 2004/01/19 00:08:45 UTC

cvs commit: jakarta-commons/dbcp/src/java/org/apache/commons/dbcp SQLNestedException.java

dirkv       2004/01/18 15:08:45

  Modified:    dbcp/src/java/org/apache/commons/dbcp
                        SQLNestedException.java
  Log:
  Bugzilla Bug 26072 Null pointer exception being thrown in SQLNestedException
  - fix nullpointer
  - enhance stacktrace printing (using some commons-lang code)
  
  Revision  Changes    Path
  1.6       +37 -17    jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/SQLNestedException.java
  
  Index: SQLNestedException.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/SQLNestedException.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SQLNestedException.java	9 Oct 2003 21:04:44 -0000	1.5
  +++ SQLNestedException.java	18 Jan 2004 23:08:45 -0000	1.6
  @@ -63,6 +63,8 @@
   
   import java.io.PrintStream;
   import java.io.PrintWriter;
  +import java.lang.reflect.Method;
  +import java.sql.DriverManager;
   import java.sql.SQLException;
   
   /**
  @@ -73,6 +75,22 @@
    */
   public class SQLNestedException extends SQLException {
   
  +    /* Throwable.getCause detection as found in commons-lang */
  +    private static final Method THROWABLE_CAUSE_METHOD;
  +    static {
  +        Method getCauseMethod;
  +        try {
  +            getCauseMethod = Throwable.class.getMethod("getCause", null);
  +        } catch (Exception e) {
  +            getCauseMethod = null;
  +        }
  +        THROWABLE_CAUSE_METHOD = getCauseMethod;
  +    }
  +    
  +    private static boolean hasThrowableCauseMethod() {
  +        return THROWABLE_CAUSE_METHOD != null;
  +    }
  +
       /**
        * Holds the reference to the exception or error that caused
        * this exception to be thrown.
  @@ -89,28 +107,30 @@
        */
       public SQLNestedException(String msg, Throwable cause) {
           super(msg);
  -        if (cause == null) {
  -            cause = new Exception("No cause");
  -        }
           this.cause = cause;
  +        if ((cause != null) && (DriverManager.getLogWriter() != null)) {
  +            DriverManager.getLogWriter().print("Caused by: ");
  +            cause.printStackTrace(DriverManager.getLogWriter());
  +        }
       }
       
       public Throwable getCause() {
           return this.cause;
       }
  -    public String getLocalizedMessage() {
  -        return super.getLocalizedMessage() + ", cause: " + this.cause.getLocalizedMessage();
  -    }
  -    public void printStackTrace() {
  -        System.err.println(getClass().getName() + ": " + getMessage() + ", cause: ");
  -        this.cause.printStackTrace();
  -    }
  +
       public void printStackTrace(PrintStream s) {
  -        s.println(getClass().getName() + ": " + getMessage() + ", cause: ");
  -        this.cause.printStackTrace(s);
  +        super.printStackTrace(s);
  +        if ((cause != null) && !hasThrowableCauseMethod()) {
  +            s.print("Caused by: ");
  +            this.cause.printStackTrace(s);
  +        }
       }
  +
       public void printStackTrace(PrintWriter s) {
  -        s.println(getClass().getName() + ": " + getMessage() + ", cause: ");
  -        this.cause.printStackTrace(s);
  +        super.printStackTrace(s);
  +        if ((cause != null) && !hasThrowableCauseMethod()) {
  +            s.print("Caused by: ");
  +            this.cause.printStackTrace(s);
  +        }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org