1. /*
  2. * @(#)InvocationTargetException.java 1.11 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang.reflect;
  8. import java.io.PrintStream;
  9. import java.io.PrintWriter;
  10. /**
  11. * InvocationTargetException is a checked exception that wraps
  12. * an exception thrown by an invoked method or constructor.
  13. *
  14. * @see Method
  15. * @see Constructor
  16. *
  17. */
  18. public
  19. class InvocationTargetException extends Exception {
  20. /**
  21. * Use serialVersionUID from JDK 1.1.X for interoperability
  22. */
  23. private static final long serialVersionUID = 4085088731926701167L;
  24. /**
  25. * This field holds the target if the
  26. * InvocationTargetException(Throwable target) constructor was
  27. * used to instantiate the object
  28. *
  29. * @serial
  30. *
  31. */
  32. private Throwable target;
  33. /**
  34. * Constructs an <code>InvocationTargetException</code> with
  35. * <code>null</code> as the target exception.
  36. */
  37. protected InvocationTargetException() {
  38. super();
  39. }
  40. /**
  41. * Constructs a InvocationTargetException with a target exception.
  42. */
  43. public InvocationTargetException(Throwable target) {
  44. super();
  45. this.target = target;
  46. }
  47. /**
  48. * Constructs a InvocationTargetException with a target exception
  49. * and a detail message.
  50. */
  51. public InvocationTargetException(Throwable target, String s) {
  52. super(s);
  53. this.target = target;
  54. }
  55. /**
  56. * Get the thrown target exception.
  57. */
  58. public Throwable getTargetException() {
  59. return target;
  60. }
  61. /**
  62. * Prints the stack trace of the thrown target exception.
  63. *
  64. * @see java.lang.System#err
  65. */
  66. public void printStackTrace() {
  67. printStackTrace(System.err);
  68. }
  69. /**
  70. * Prints the stack trace of the thrown target exception to the specified
  71. * print stream.
  72. */
  73. public void printStackTrace(PrintStream ps) {
  74. synchronized (ps) {
  75. if (target != null) {
  76. ps.print("java.lang.reflect.InvocationTargetException: ");
  77. target.printStackTrace(ps);
  78. } else {
  79. super.printStackTrace(ps);
  80. }
  81. }
  82. }
  83. /**
  84. * Prints the stack trace of the thrown target exception to the
  85. * specified print writer.
  86. */
  87. public void printStackTrace(PrintWriter pw) {
  88. synchronized (pw) {
  89. if (target != null) {
  90. pw.print("java.lang.reflect.InvocationTargetException: ");
  91. target.printStackTrace(pw);
  92. } else {
  93. super.printStackTrace(pw);
  94. }
  95. }
  96. }
  97. }