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