1. /*
  2. * @(#)InvocationTargetException.java 1.19 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. * InvocationTargetException is a checked exception that wraps
  10. * an exception thrown by an invoked method or constructor.
  11. *
  12. * <p>As of release 1.4, this exception has been retrofitted to conform to
  13. * the general purpose exception-chaining mechanism. The "target exception"
  14. * that is provided at construction time and accessed via the
  15. * {@link #getTargetException()} method is now known as the <i>cause</i>,
  16. * and may be accessed via the {@link Throwable#getCause()} method,
  17. * as well as the aforementioned "legacy method."
  18. *
  19. * @see Method
  20. * @see Constructor
  21. */
  22. public class InvocationTargetException extends Exception {
  23. /**
  24. * Use serialVersionUID from JDK 1.1.X for interoperability
  25. */
  26. private static final long serialVersionUID = 4085088731926701167L;
  27. /**
  28. * This field holds the target if the
  29. * InvocationTargetException(Throwable target) constructor was
  30. * used to instantiate the object
  31. *
  32. * @serial
  33. *
  34. */
  35. private Throwable target;
  36. /**
  37. * Constructs an <code>InvocationTargetException</code> with
  38. * <code>null</code> as the target exception.
  39. */
  40. protected InvocationTargetException() {
  41. super((Throwable)null); // Disallow initCause
  42. }
  43. /**
  44. * Constructs a InvocationTargetException with a target exception.
  45. *
  46. * @param target the target exception
  47. */
  48. public InvocationTargetException(Throwable target) {
  49. super((Throwable)null); // Disallow initCause
  50. this.target = target;
  51. }
  52. /**
  53. * Constructs a InvocationTargetException with a target exception
  54. * and a detail message.
  55. *
  56. * @param target the target exception
  57. * @param s the detail message
  58. */
  59. public InvocationTargetException(Throwable target, String s) {
  60. super(s, null); // Disallow initCause
  61. this.target = target;
  62. }
  63. /**
  64. * Get the thrown target exception.
  65. *
  66. * <p>This method predates the general-purpose exception chaining facility.
  67. * The {@link Throwable#getCause()} method is now the preferred means of
  68. * obtaining this information.
  69. *
  70. * @return the thrown target exception (cause of this exception).
  71. */
  72. public Throwable getTargetException() {
  73. return target;
  74. }
  75. /**
  76. * Returns the cause of this exception (the thrown target exception,
  77. * which may be <tt>null</tt>).
  78. *
  79. * @return the cause of this exception.
  80. * @since 1.4
  81. */
  82. public Throwable getCause() {
  83. return target;
  84. }
  85. }