1. /*
  2. * @(#)InvocationTargetException.java 1.12 00/02/02
  3. *
  4. * Copyright 1996-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. * InvocationTargetException is a checked exception that wraps
  15. * an exception thrown by an invoked method or constructor.
  16. *
  17. * @see Method
  18. * @see Constructor
  19. *
  20. */
  21. public
  22. 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();
  42. }
  43. /**
  44. * Constructs a InvocationTargetException with a target exception.
  45. */
  46. public InvocationTargetException(Throwable target) {
  47. super();
  48. this.target = target;
  49. }
  50. /**
  51. * Constructs a InvocationTargetException with a target exception
  52. * and a detail message.
  53. */
  54. public InvocationTargetException(Throwable target, String s) {
  55. super(s);
  56. this.target = target;
  57. }
  58. /**
  59. * Get the thrown target exception.
  60. */
  61. public Throwable getTargetException() {
  62. return target;
  63. }
  64. /**
  65. * Prints the stack trace of the thrown target exception.
  66. *
  67. * @see java.lang.System#err
  68. */
  69. public void printStackTrace() {
  70. printStackTrace(System.err);
  71. }
  72. /**
  73. * Prints the stack trace of the thrown target exception to the specified
  74. * print stream.
  75. */
  76. public void printStackTrace(PrintStream ps) {
  77. synchronized (ps) {
  78. if (target != null) {
  79. ps.print("java.lang.reflect.InvocationTargetException: ");
  80. target.printStackTrace(ps);
  81. } else {
  82. super.printStackTrace(ps);
  83. }
  84. }
  85. }
  86. /**
  87. * Prints the stack trace of the thrown target exception to the
  88. * specified print writer.
  89. */
  90. public void printStackTrace(PrintWriter pw) {
  91. synchronized (pw) {
  92. if (target != null) {
  93. pw.print("java.lang.reflect.InvocationTargetException: ");
  94. target.printStackTrace(pw);
  95. } else {
  96. super.printStackTrace(pw);
  97. }
  98. }
  99. }
  100. }