1. /*
  2. * @(#)ExceptionInInitializerError.java 1.10 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;
  8. import java.io.PrintStream;
  9. import java.io.PrintWriter;
  10. /**
  11. * Signals that an unexpected exception has occurred in a static initializer.
  12. * An <code>ExceptionInInitializerError</code> is thrown to indicate that an
  13. * exception occurred during evaluation of a static initializer or the
  14. * initializer for a static variable.
  15. *
  16. * @author Frank Yellin
  17. * @version 1.10, 11/29/01
  18. *
  19. * @since JDK1.1
  20. */
  21. public
  22. class ExceptionInInitializerError extends LinkageError {
  23. /**
  24. * Use serialVersionUID from JDK 1.1.X for interoperability
  25. */
  26. private static final long serialVersionUID = 1521711792217232256L;
  27. /**
  28. * This field holds the exception if the
  29. * ExceptionInInitializerError(Throwable thrown) constructor was
  30. * used to instantiate the object
  31. *
  32. * @serial
  33. *
  34. */
  35. private Throwable exception;
  36. /**
  37. * Constructs an <code>ExceptionInInitializerError</code> with
  38. * <code>null</code> as its detail message string and with no saved
  39. * thowable object.
  40. * A detail message is a String that describes this particular exception.
  41. */
  42. public ExceptionInInitializerError() {
  43. super();
  44. }
  45. /**
  46. * Constructs a new <code>ExceptionInInitializerError</code> class by
  47. * saving a reference to the <code>Throwable</code> object thrown for
  48. * later retrieval by the {@link #getException()} method. The detail
  49. * message string is set to <code>null</code>.
  50. *
  51. * @param thrown The exception thrown
  52. */
  53. public ExceptionInInitializerError(Throwable thrown) {
  54. this.exception = thrown;
  55. }
  56. /**
  57. * Constructs an ExceptionInInitializerError with the specified detail
  58. * message string. A detail message is a String that describes this
  59. * particular exception. The detail message string is saved for later
  60. * retrieval by the {@link Throwable#getMessage()} method. There is no
  61. * saved throwable object.
  62. *
  63. *
  64. * @param s the detail message
  65. */
  66. public ExceptionInInitializerError(String s) {
  67. super(s);
  68. }
  69. /**
  70. * Returns the exception that occurred during a static initialization that
  71. * caused this Error to be created.
  72. *
  73. * @return the saved throwable object of this
  74. * <code>ExceptionInInitializerError</code>, or <code>null</code>
  75. * if this <code>ExceptionInInitializerError</code> has no saved
  76. * throwable object.
  77. */
  78. public Throwable getException() {
  79. return exception;
  80. }
  81. /**
  82. * Prints the stack trace of the exception that occurred.
  83. *
  84. * @see java.lang.System#err
  85. */
  86. public void printStackTrace() {
  87. printStackTrace(System.err);
  88. }
  89. /**
  90. * Prints the stack trace of the exception that occurred to the
  91. * specified print stream.
  92. */
  93. public void printStackTrace(PrintStream ps) {
  94. synchronized (ps) {
  95. if (exception != null) {
  96. ps.print("java.lang.ExceptionInInitializerError: ");
  97. exception.printStackTrace(ps);
  98. } else {
  99. super.printStackTrace(ps);
  100. }
  101. }
  102. }
  103. /**
  104. * Prints the stack trace of the exception that occurred to the
  105. * specified print writer.
  106. */
  107. public void printStackTrace(PrintWriter pw) {
  108. synchronized (pw) {
  109. if (exception != null) {
  110. pw.print("java.lang.ExceptionInInitializerError: ");
  111. exception.printStackTrace(pw);
  112. } else {
  113. super.printStackTrace(pw);
  114. }
  115. }
  116. }
  117. }