1 16 17 21 22 package javax.jdo; 23 24 import javax.jdo.spi.I18NHelper; 25 26 31 public class JDOException extends java.lang.RuntimeException { 32 33 36 Throwable [] nested; 37 38 43 Object failed; 44 45 47 private static I18NHelper msg = I18NHelper.getInstance ("javax.jdo.Bundle"); 49 51 private boolean inPrintStackTrace = false; 52 53 56 public JDOException() { 57 } 58 59 60 64 public JDOException(String msg) { 65 super(msg); 66 } 67 68 73 public JDOException(String msg, Throwable [] nested) { 74 super(msg); 75 this.nested = nested; 76 } 77 78 83 public JDOException(String msg, Throwable nested) { 84 super(msg); 85 this.nested = new Throwable [] {nested}; 86 } 87 88 93 public JDOException(String msg, Object failed) { 94 super(msg); 95 this.failed = failed; 96 } 97 98 104 public JDOException(String msg, Throwable [] nested, Object failed) { 105 super(msg); 106 this.nested = nested; 107 this.failed = failed; 108 } 109 110 116 public JDOException(String msg, Throwable nested, Object failed) { 117 super(msg); 118 this.nested = new Throwable [] {nested}; 119 this.failed = failed; 120 } 121 122 125 public Object getFailedObject() { 126 return failed; 127 } 128 129 134 public Throwable [] getNestedExceptions() { 135 return nested; 136 } 137 138 144 public synchronized Throwable getCause() { 145 if (nested == null || nested.length == 0 || inPrintStackTrace) { 150 return null; 151 } else { 152 return nested[0]; 153 } 154 } 155 156 162 public Throwable initCause(Throwable cause) { 163 throw new JDOFatalInternalException(msg.msg("ERR_CannotInitCause")); 164 } 165 166 172 public synchronized String toString() { 173 int len = nested==null?0:nested.length; 174 StringBuffer sb = new StringBuffer (10 + 100 * len); 176 sb.append (super.toString()); 177 if (failed != null) { 179 sb.append ("\n").append (msg.msg ("MSG_FailedObject")); 180 String failedToString = null; 181 try { 182 failedToString = failed.toString(); 183 } catch (Exception ex) { 184 Object objectId = JDOHelper.getObjectId(failed); 186 if (objectId == null) { 187 failedToString = msg.msg("MSG_ExceptionGettingFailedToString", exceptionToString(ex)); 189 } 190 else { 191 String objectIdToString = null; 193 try { 194 objectIdToString = objectId.toString(); 195 } 196 catch (Exception ex2) { 197 objectIdToString = exceptionToString(ex2); 198 } 199 failedToString = msg.msg("MSG_ExceptionGettingFailedToStringObjectId", exceptionToString(ex), objectIdToString); 201 } 202 } 203 sb.append (failedToString); 204 } 205 if (len > 0 && !inPrintStackTrace) { 208 sb.append ("\n").append (msg.msg ("MSG_NestedThrowables")).append ("\n"); 209 Throwable exception = nested[0]; 210 sb.append (exception==null?"null":exception.toString()); for (int i=1; i<len; ++i) { 212 sb.append ("\n"); exception = nested[i]; 214 sb.append (exception==null?"null":exception.toString()); } 216 } 217 return sb.toString(); 218 } 219 220 225 public void printStackTrace() { 226 printStackTrace (System.err); 227 } 228 229 235 public synchronized void printStackTrace(java.io.PrintStream s) { 236 int len = nested==null?0:nested.length; 237 synchronized (s) { 238 inPrintStackTrace = true; 239 super.printStackTrace(s); 240 if (len > 0) { 241 s.println (msg.msg ("MSG_NestedThrowablesStackTrace")); 242 for (int i=0; i<len; ++i) { 243 Throwable exception = nested[i]; 244 if (exception != null) { 245 exception.printStackTrace(s); 246 } 247 } 248 } 249 inPrintStackTrace = false; 250 } 251 } 252 253 259 public synchronized void printStackTrace(java.io.PrintWriter s) { 260 int len = nested==null?0:nested.length; 261 synchronized (s) { 262 inPrintStackTrace = true; 263 super.printStackTrace(s); 264 if (len > 0) { 265 s.println (msg.msg ("MSG_NestedThrowablesStackTrace")); 266 for (int i=0; i<len; ++i) { 267 Throwable exception = nested[i]; 268 if (exception != null) { 269 exception.printStackTrace(s); 270 } 271 } 272 } 273 inPrintStackTrace = false; 274 } 275 } 276 277 287 private static String exceptionToString(Exception ex) 288 { 289 if (ex == null) return null; 290 String s = ex.getClass().getName(); 291 String message = ex.getMessage(); 292 return (message != null) ? (s + ": " + message) : s; 293 } 294 } 295 296 | Popular Tags |