1 7 8 package java.lang; 9 import java.io.*; 10 11 130 public class Throwable implements Serializable { 131 132 private static final long serialVersionUID = -3042686055658047285L; 133 134 137 private transient Object backtrace; 138 139 146 private String detailMessage; 147 148 158 private Throwable cause = this; 159 160 166 private StackTraceElement [] stackTrace; 167 171 172 180 public Throwable() { 181 fillInStackTrace(); 182 } 183 184 195 public Throwable(String message) { 196 fillInStackTrace(); 197 detailMessage = message; 198 } 199 200 217 public Throwable(String message, Throwable cause) { 218 fillInStackTrace(); 219 detailMessage = message; 220 this.cause = cause; 221 } 222 223 240 public Throwable(Throwable cause) { 241 fillInStackTrace(); 242 detailMessage = (cause==null ? null : cause.toString()); 243 this.cause = cause; 244 } 245 246 252 public String getMessage() { 253 return detailMessage; 254 } 255 256 266 public String getLocalizedMessage() { 267 return getMessage(); 268 } 269 270 290 public Throwable getCause() { 291 return (cause==this ? null : cause); 292 } 293 294 318 public synchronized Throwable initCause(Throwable cause) { 319 if (this.cause != this) 320 throw new IllegalStateException ("Can't overwrite cause"); 321 if (cause == this) 322 throw new IllegalArgumentException ("Self-causation not permitted"); 323 this.cause = cause; 324 return this; 325 } 326 327 342 public String toString() { 343 String s = getClass().getName(); 344 String message = getLocalizedMessage(); 345 return (message != null) ? (s + ": " + message) : s; 346 } 347 348 451 public void printStackTrace() { 452 printStackTrace(System.err); 453 } 454 455 460 public void printStackTrace(PrintStream s) { 461 synchronized (s) { 462 s.println(this); 463 StackTraceElement [] trace = getOurStackTrace(); 464 for (int i=0; i < trace.length; i++) 465 s.println("\tat " + trace[i]); 466 467 Throwable ourCause = getCause(); 468 if (ourCause != null) 469 ourCause.printStackTraceAsCause(s, trace); 470 } 471 } 472 473 476 private void printStackTraceAsCause(PrintStream s, 477 StackTraceElement [] causedTrace) 478 { 479 481 StackTraceElement [] trace = getOurStackTrace(); 483 int m = trace.length-1, n = causedTrace.length-1; 484 while (m >= 0 && n >=0 && trace[m].equals(causedTrace[n])) { 485 m--; n--; 486 } 487 int framesInCommon = trace.length - 1 - m; 488 489 s.println("Caused by: " + this); 490 for (int i=0; i <= m; i++) 491 s.println("\tat " + trace[i]); 492 if (framesInCommon != 0) 493 s.println("\t... " + framesInCommon + " more"); 494 495 Throwable ourCause = getCause(); 497 if (ourCause != null) 498 ourCause.printStackTraceAsCause(s, trace); 499 } 500 501 508 public void printStackTrace(PrintWriter s) { 509 synchronized (s) { 510 s.println(this); 511 StackTraceElement [] trace = getOurStackTrace(); 512 for (int i=0; i < trace.length; i++) 513 s.println("\tat " + trace[i]); 514 515 Throwable ourCause = getCause(); 516 if (ourCause != null) 517 ourCause.printStackTraceAsCause(s, trace); 518 } 519 } 520 521 524 private void printStackTraceAsCause(PrintWriter s, 525 StackTraceElement [] causedTrace) 526 { 527 529 StackTraceElement [] trace = getOurStackTrace(); 531 int m = trace.length-1, n = causedTrace.length-1; 532 while (m >= 0 && n >=0 && trace[m].equals(causedTrace[n])) { 533 m--; n--; 534 } 535 int framesInCommon = trace.length - 1 - m; 536 537 s.println("Caused by: " + this); 538 for (int i=0; i <= m; i++) 539 s.println("\tat " + trace[i]); 540 if (framesInCommon != 0) 541 s.println("\t... " + framesInCommon + " more"); 542 543 Throwable ourCause = getCause(); 545 if (ourCause != null) 546 ourCause.printStackTraceAsCause(s, trace); 547 } 548 549 557 public synchronized native Throwable fillInStackTrace(); 558 559 582 public StackTraceElement [] getStackTrace() { 583 return (StackTraceElement []) getOurStackTrace().clone(); 584 } 585 586 private synchronized StackTraceElement [] getOurStackTrace() { 587 if (stackTrace == null) { 589 int depth = getStackTraceDepth(); 590 stackTrace = new StackTraceElement [depth]; 591 for (int i=0; i < depth; i++) 592 stackTrace[i] = getStackTraceElement(i); 593 } 594 return stackTrace; 595 } 596 597 620 public void setStackTrace(StackTraceElement [] stackTrace) { 621 StackTraceElement [] defensiveCopy = 622 (StackTraceElement []) stackTrace.clone(); 623 for (int i = 0; i < defensiveCopy.length; i++) 624 if (defensiveCopy[i] == null) 625 throw new NullPointerException ("stackTrace[" + i + "]"); 626 627 this.stackTrace = defensiveCopy; 628 } 629 630 634 private native int getStackTraceDepth(); 635 636 643 private native StackTraceElement getStackTraceElement(int index); 644 645 private synchronized void writeObject(java.io.ObjectOutputStream s) 646 throws IOException 647 { 648 getOurStackTrace(); s.defaultWriteObject(); 650 } 651 } 652 | Popular Tags |