1. /*
  2. * @(#)UndeclaredThrowableException.java 1.13 04/02/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang.reflect;
  8. /**
  9. * Thrown by a method invocation on a proxy instance if its invocation
  10. * handler's {@link InvocationHandler#invoke invoke} method throws a
  11. * checked exception (a <code>Throwable</code> that is not assignable
  12. * to <code>RuntimeException</code> or <code>Error</code>) that
  13. * is not assignable to any of the exception types declared in the
  14. * <code>throws</code> clause of the method that was invoked on the
  15. * proxy instance and dispatched to the invocation handler.
  16. *
  17. * <p>An <code>UndeclaredThrowableException</code> instance contains
  18. * the undeclared checked exception that was thrown by the invocation
  19. * handler, and it can be retrieved with the
  20. * <code>getUndeclaredThrowable()</code> method.
  21. * <code>UndeclaredThrowableException</code> extends
  22. * <code>RuntimeException</code>, so it is an unchecked exception
  23. * that wraps a checked exception.
  24. *
  25. * <p>As of release 1.4, this exception has been retrofitted to
  26. * conform to the general purpose exception-chaining mechanism. The
  27. * "undeclared checked exception that was thrown by the invocation
  28. * handler" that may be provided at construction time and accessed via
  29. * the {@link #getUndeclaredThrowable()} method is now known as the
  30. * <i>cause</i>, and may be accessed via the {@link
  31. * Throwable#getCause()} method, as well as the aforementioned "legacy
  32. * method."
  33. *
  34. * @author Peter Jones
  35. * @version 1.13, 04/02/19
  36. * @see InvocationHandler
  37. * @since JDK1.3
  38. */
  39. public class UndeclaredThrowableException extends RuntimeException {
  40. static final long serialVersionUID = 330127114055056639L;
  41. /**
  42. * the undeclared checked exception that was thrown
  43. * @serial
  44. */
  45. private Throwable undeclaredThrowable;
  46. /**
  47. * Constructs an <code>UndeclaredThrowableException</code> with the
  48. * specified <code>Throwable</code>.
  49. *
  50. * @param undeclaredThrowable the undeclared checked exception
  51. * that was thrown
  52. */
  53. public UndeclaredThrowableException(Throwable undeclaredThrowable) {
  54. super((Throwable) null); // Disallow initCause
  55. this.undeclaredThrowable = undeclaredThrowable;
  56. }
  57. /**
  58. * Constructs an <code>UndeclaredThrowableException</code> with the
  59. * specified <code>Throwable</code> and a detail message.
  60. *
  61. * @param undeclaredThrowable the undeclared checked exception
  62. * that was thrown
  63. * @param s the detail message
  64. */
  65. public UndeclaredThrowableException(Throwable undeclaredThrowable,
  66. String s)
  67. {
  68. super(s, null); // Disallow initCause
  69. this.undeclaredThrowable = undeclaredThrowable;
  70. }
  71. /**
  72. * Returns the <code>Throwable</code> instance wrapped in this
  73. * <code>UndeclaredThrowableException</code>, which may be <tt>null</tt>.
  74. *
  75. * <p>This method predates the general-purpose exception chaining facility.
  76. * The {@link Throwable#getCause()} method is now the preferred means of
  77. * obtaining this information.
  78. *
  79. * @return the undeclared checked exception that was thrown
  80. */
  81. public Throwable getUndeclaredThrowable() {
  82. return undeclaredThrowable;
  83. }
  84. /**
  85. * Returns the cause of this exception (the <code>Throwable</code>
  86. * instance wrapped in this <code>UndeclaredThrowableException</code>,
  87. * which may be <tt>null</tt>).
  88. *
  89. * @return the cause of this exception.
  90. * @since 1.4
  91. */
  92. public Throwable getCause() {
  93. return undeclaredThrowable;
  94. }
  95. }