449 * unknown.) 450 * @return a reference to this {@code Throwable} instance. 451 * @throws IllegalArgumentException if {@code cause} is this 452 * throwable. (A throwable cannot be its own cause.) 453 * @throws IllegalStateException if this throwable was 454 * created with {@link #Throwable(Throwable)} or 455 * {@link #Throwable(String,Throwable)}, or this method has already 456 * been called on this throwable. 457 * @since 1.4 458 */ 459 public synchronized Throwable initCause(Throwable cause) { 460 if (this.cause != this) 461 throw new IllegalStateException("Can't overwrite cause with " + 462 Objects.toString(cause, "a null"), this); 463 if (cause == this) 464 throw new IllegalArgumentException("Self-causation not permitted", this); 465 this.cause = cause; 466 return this; 467 } 468 469 /** 470 * Returns a short description of this throwable. 471 * The result is the concatenation of: 472 * <ul> 473 * <li> the {@linkplain Class#getName() name} of the class of this object 474 * <li> ": " (a colon and a space) 475 * <li> the result of invoking this object's {@link #getLocalizedMessage} 476 * method 477 * </ul> 478 * If {@code getLocalizedMessage} returns {@code null}, then just 479 * the class name is returned. 480 * 481 * @return a string representation of this throwable. 482 */ 483 public String toString() { 484 String s = getClass().getName(); 485 String message = getLocalizedMessage(); 486 return (message != null) ? (s + ": " + message) : s; 487 } 488 | 449 * unknown.) 450 * @return a reference to this {@code Throwable} instance. 451 * @throws IllegalArgumentException if {@code cause} is this 452 * throwable. (A throwable cannot be its own cause.) 453 * @throws IllegalStateException if this throwable was 454 * created with {@link #Throwable(Throwable)} or 455 * {@link #Throwable(String,Throwable)}, or this method has already 456 * been called on this throwable. 457 * @since 1.4 458 */ 459 public synchronized Throwable initCause(Throwable cause) { 460 if (this.cause != this) 461 throw new IllegalStateException("Can't overwrite cause with " + 462 Objects.toString(cause, "a null"), this); 463 if (cause == this) 464 throw new IllegalArgumentException("Self-causation not permitted", this); 465 this.cause = cause; 466 return this; 467 } 468 469 /* 470 * This is called by readObject of a few exceptions such as 471 * ClassNotFoundException and ExceptionInInitializerError to deserialize 472 * a stream output from an older runtime version where the cause may 473 * have set to null. 474 */ 475 void setCause(Throwable t) { 476 this.cause = t; 477 } 478 479 /** 480 * Returns a short description of this throwable. 481 * The result is the concatenation of: 482 * <ul> 483 * <li> the {@linkplain Class#getName() name} of the class of this object 484 * <li> ": " (a colon and a space) 485 * <li> the result of invoking this object's {@link #getLocalizedMessage} 486 * method 487 * </ul> 488 * If {@code getLocalizedMessage} returns {@code null}, then just 489 * the class name is returned. 490 * 491 * @return a string representation of this throwable. 492 */ 493 public String toString() { 494 String s = getClass().getName(); 495 String message = getLocalizedMessage(); 496 return (message != null) ? (s + ": " + message) : s; 497 } 498 |