1 9 package org.openuss.utility; 10 11 import java.io.PrintWriter ; 12 13 22 import java.io.StringWriter ; 23 24 25 public class NestingException extends Exception { 26 private Throwable nestedException; 28 29 private String stackTraceString; 31 32 35 public NestingException() { 36 } 37 38 41 public NestingException(String msg) { 42 super(msg); 43 } 44 45 48 public NestingException(Throwable nestedException) { 49 this.nestedException = nestedException; 50 stackTraceString = generateStackTraceString(nestedException); 51 } 52 53 56 public NestingException(String msg, Throwable nestedException) { 57 this(msg); 58 this.nestedException = nestedException; 59 stackTraceString = generateStackTraceString(nestedException); 60 } 61 62 65 static public String generateStackTraceString(Throwable t) { 66 StringWriter s = new StringWriter (); 67 t.printStackTrace(new PrintWriter (s)); 68 69 return s.toString(); 70 } 71 72 75 public Throwable getNestedException() { 76 return nestedException; 77 } 78 79 83 public String getStackTraceString() { 84 if (nestedException == null) { 86 return null; 87 } 88 89 StringBuffer traceBuffer = new StringBuffer (); 90 91 if (nestedException instanceof NestingException) { 92 traceBuffer.append( 93 ((NestingException) nestedException).getStackTraceString()); 94 traceBuffer.append("-------- nested by:\n"); 95 } 96 97 traceBuffer.append(stackTraceString); 98 99 return traceBuffer.toString(); 100 } 101 102 105 public String getMessage() { 106 String superMsg = super.getMessage(); 109 110 if (getNestedException() == null) { 112 return superMsg; 113 } 114 115 StringBuffer theMsg = new StringBuffer (); 116 117 String nestedMsg = getNestedException().getMessage(); 119 120 if (superMsg != null) { 121 theMsg.append(superMsg).append(": ").append(nestedMsg); 122 } else { 123 theMsg.append(nestedMsg); 124 } 125 126 return theMsg.toString(); 127 } 128 129 132 public String toString() { 133 StringBuffer theMsg = new StringBuffer (super.toString()); 134 135 if (getNestedException() != null) { 136 theMsg.append("; \n\t---> nested ").append(getNestedException()); 137 } 138 139 return theMsg.toString(); 140 } 141 } | Popular Tags |