1 package in.co.daffodil.db.general; 2 3 import java.sql.SQLException ; 4 import com.daffodilwoods.database.resource.*; 5 import java.sql.DriverManager ; 6 import java.io.*; 7 8 public class DaffodilDBExceptionWrapper 9 extends SQLException { 10 public static final long serialVersionUID = -5113675351842916894L; 11 Exception exception; 12 static boolean appendToMessage = true; 13 14 public DaffodilDBExceptionWrapper(String reason, Exception exception) { 15 super(reason); 16 if (DriverManager.getLogWriter() != null) { 17 DriverManager.println("Chained exception : " + reason); 18 if (exception != null) 19 exception.printStackTrace(DriverManager.getLogWriter()); 20 } 21 this.exception = exception; 22 } 23 24 public SQLException append(Exception E) { 25 exception = E; 26 return this; 27 } 28 29 public void printStackTrace(PrintStream s) { 30 super.printStackTrace(s); 31 s.println("Wrapped Exception"); 32 if (exception != null) 33 exception.printStackTrace(s); 34 } 35 36 public void printStackTrace(PrintWriter s) { 37 super.printStackTrace(s); 38 s.println("Wrapped Exception"); 39 if (exception != null) 40 exception.printStackTrace(s); 41 } 42 43 public String toString() { 44 return super.toString() + "Wrapped[" + this.exception + "]"; 45 } 46 47 public String getMessage() { 48 String s = super.getMessage(); 49 if (appendToMessage && this.exception != null) 50 s += "\t" + exceptionStackTraceString(this.exception); 51 return s; 52 } 53 54 public String getSQLState() { 55 String s = super.getSQLState(); 56 if (s == null) 57 return ""; 58 return s; 59 } 60 61 public static String exceptionStackTraceString(Exception e) { 62 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 63 PrintStream ps = new PrintStream(bos); 64 if (e != null) 65 e.printStackTrace(ps); 66 ps.flush(); 67 String s = new String (bos.toByteArray()); 68 return s; 69 } 70 } 71 | Popular Tags |