1. /*
  2. * @(#)UndeclaredThrowableException.java 1.6 00/02/02
  3. *
  4. * Copyright 1999, 2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.lang.reflect;
  11. import java.io.PrintStream;
  12. import java.io.PrintWriter;
  13. /**
  14. * Thrown by a method invocation on a proxy instance if its invocation
  15. * handler's {@link InvocationHandler#invoke invoke} method throws a
  16. * checked exception (a <code>Throwable</code> that is not assignable
  17. * to <code>RuntimeException</code> or <code>Error</code>) that
  18. * is not assignable to any of the exception types declared in the
  19. * <code>throws</code> clause of the method that was invoked on the
  20. * proxy instance and dispatched to the invocation handler.
  21. *
  22. * <p>An <code>UndeclaredThrowableException</code> instance contains
  23. * the undeclared checked exception that was thrown by the invocation
  24. * handler, and it can be retrieved with the
  25. * <code>getUndeclaredThrowable()</code> method.
  26. * <code>UndeclaredThrowableException</code> extends
  27. * <code>RuntimeException</code>, so it is an unchecked exception
  28. * that wraps a checked exception.
  29. *
  30. * @author Peter Jones
  31. * @version 1.6, 00/02/02
  32. * @see InvocationHandler
  33. * @since JDK1.3
  34. */
  35. public class UndeclaredThrowableException extends RuntimeException {
  36. /**
  37. * the undeclared checked exception that was thrown
  38. * @serial
  39. */
  40. private Throwable undeclaredThrowable;
  41. /**
  42. * Constructs an <code>UndeclaredThrowableException</code> with the
  43. * specifed <code>Throwable</code>.
  44. *
  45. * @param undeclaredThrowable the undeclared checked exception
  46. * that was thrown
  47. */
  48. public UndeclaredThrowableException(Throwable undeclaredThrowable) {
  49. super();
  50. this.undeclaredThrowable = undeclaredThrowable;
  51. }
  52. /**
  53. * Constructs an <code>UndeclaredThrowableException</code> with the
  54. * specified <code>Throwable</code> and a detail message.
  55. *
  56. * @param undeclaredThrowable the undeclared checked exception
  57. * that was thrown
  58. * @param s the detail message
  59. */
  60. public UndeclaredThrowableException(Throwable undeclaredThrowable,
  61. String s)
  62. {
  63. super(s);
  64. this.undeclaredThrowable = undeclaredThrowable;
  65. }
  66. /**
  67. * Returns the <code>Throwable</code> instance wrapped in this
  68. * <code>UndeclaredThrowableException</code>.
  69. *
  70. * @return the undeclared checked exception that was thrown
  71. */
  72. public Throwable getUndeclaredThrowable() {
  73. return undeclaredThrowable;
  74. }
  75. /**
  76. * Prints this <code>UndeclaredThrowableException</code> and its
  77. * backtrace to the standard error stream.
  78. */
  79. public void printStackTrace() {
  80. printStackTrace(System.err);
  81. }
  82. /**
  83. * Prints this <code>UndeclaredThrowableException</code> and its
  84. * backtrace to the specified <code>PrintStream</code>.
  85. */
  86. public void printStackTrace(PrintStream ps) {
  87. synchronized (ps) {
  88. if (undeclaredThrowable != null) {
  89. ps.print("java.lang.reflect.UndeclaredThrowableException: ");
  90. undeclaredThrowable.printStackTrace(ps);
  91. } else {
  92. super.printStackTrace(ps);
  93. }
  94. }
  95. }
  96. /**
  97. * Prints this <code>UndeclaredThrowableException</code> and its
  98. * backtrace to the specified <code>PrintWriter</code>.
  99. */
  100. public void printStackTrace(PrintWriter pw) {
  101. synchronized (pw) {
  102. if (undeclaredThrowable != null) {
  103. pw.print("java.lang.reflect.UndeclaredThrowableException: ");
  104. undeclaredThrowable.printStackTrace(pw);
  105. } else {
  106. super.printStackTrace(pw);
  107. }
  108. }
  109. }
  110. }