1. /*
  2. * @(#)ExceptionInInitializerError.java 1.17 03/12/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;
  8. /**
  9. * Signals that an unexpected exception has occurred in a static initializer.
  10. * An <code>ExceptionInInitializerError</code> is thrown to indicate that an
  11. * exception occurred during evaluation of a static initializer or the
  12. * initializer for a static variable.
  13. *
  14. * <p>As of release 1.4, this exception has been retrofitted to conform to
  15. * the general purpose exception-chaining mechanism. The "saved throwable
  16. * object" that may be provided at construction time and accessed via
  17. * the {@link #getException()} method is now known as the <i>cause</i>,
  18. * and may be accessed via the {@link Throwable#getCause()} method, as well
  19. * as the aforementioned "legacy method."
  20. *
  21. * @author Frank Yellin
  22. * @version 1.17, 12/19/03
  23. * @since JDK1.1
  24. */
  25. public 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. * throwable object.
  43. * A detail message is a String that describes this particular exception.
  44. */
  45. public ExceptionInInitializerError() {
  46. initCause(null); // Disallow subsequent initCause
  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. initCause(null); // Disallow subsequent initCause
  58. this.exception = thrown;
  59. }
  60. /**
  61. * Constructs an ExceptionInInitializerError with the specified detail
  62. * message string. A detail message is a String that describes this
  63. * particular exception. The detail message string is saved for later
  64. * retrieval by the {@link Throwable#getMessage()} method. There is no
  65. * saved throwable object.
  66. *
  67. *
  68. * @param s the detail message
  69. */
  70. public ExceptionInInitializerError(String s) {
  71. super(s);
  72. initCause(null); // Disallow subsequent initCause
  73. }
  74. /**
  75. * Returns the exception that occurred during a static initialization that
  76. * caused this error to be created.
  77. *
  78. * <p>This method predates the general-purpose exception chaining facility.
  79. * The {@link Throwable#getCause()} method is now the preferred means of
  80. * obtaining this information.
  81. *
  82. * @return the saved throwable object of this
  83. * <code>ExceptionInInitializerError</code>, or <code>null</code>
  84. * if this <code>ExceptionInInitializerError</code> has no saved
  85. * throwable object.
  86. */
  87. public Throwable getException() {
  88. return exception;
  89. }
  90. /**
  91. * Returns the cause of this error (the exception that occurred
  92. * during a static initialization that caused this error to be created).
  93. *
  94. * @return the cause of this error or <code>null</code> if the
  95. * cause is nonexistent or unknown.
  96. * @since 1.4
  97. */
  98. public Throwable getCause() {
  99. return exception;
  100. }
  101. }