src/java.base/share/classes/java/lang/ExceptionInInitializerError.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 51772 5432cebf6627
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 1996, 2000, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    30 import java.io.ObjectOutputStream;
    30 import java.io.ObjectOutputStream;
    31 import java.io.ObjectStreamField;
    31 import java.io.ObjectStreamField;
    32 
    32 
    33 /**
    33 /**
    34  * Signals that an unexpected exception has occurred in a static initializer.
    34  * Signals that an unexpected exception has occurred in a static initializer.
    35  * An <code>ExceptionInInitializerError</code> is thrown to indicate that an
    35  * An {@code ExceptionInInitializerError} is thrown to indicate that an
    36  * exception occurred during evaluation of a static initializer or the
    36  * exception occurred during evaluation of a static initializer or the
    37  * initializer for a static variable.
    37  * initializer for a static variable.
    38  *
    38  *
    39  * <p>As of release 1.4, this exception has been retrofitted to conform to
    39  * <p>As of release 1.4, this exception has been retrofitted to conform to
    40  * the general purpose exception-chaining mechanism.  The "saved throwable
    40  * the general purpose exception-chaining mechanism.  The "saved throwable
    48  */
    48  */
    49 public class ExceptionInInitializerError extends LinkageError {
    49 public class ExceptionInInitializerError extends LinkageError {
    50     /**
    50     /**
    51      * Use serialVersionUID from JDK 1.1.X for interoperability
    51      * Use serialVersionUID from JDK 1.1.X for interoperability
    52      */
    52      */
       
    53     @java.io.Serial
    53     private static final long serialVersionUID = 1521711792217232256L;
    54     private static final long serialVersionUID = 1521711792217232256L;
    54 
    55 
    55     /**
    56     /**
    56      * Constructs an <code>ExceptionInInitializerError</code> with
    57      * Constructs an {@code ExceptionInInitializerError} with
    57      * <code>null</code> as its detail message string and with no saved
    58      * {@code null} as its detail message string and with no saved
    58      * throwable object.
    59      * throwable object.
    59      * A detail message is a String that describes this particular exception.
    60      * A detail message is a String that describes this particular exception.
    60      */
    61      */
    61     public ExceptionInInitializerError() {
    62     public ExceptionInInitializerError() {
    62         initCause(null); // Disallow subsequent initCause
    63         initCause(null); // Disallow subsequent initCause
    63     }
    64     }
    64 
    65 
    65     /**
    66     /**
    66      * Constructs a new <code>ExceptionInInitializerError</code> class by
    67      * Constructs a new {@code ExceptionInInitializerError} class by
    67      * saving a reference to the <code>Throwable</code> object thrown for
    68      * saving a reference to the {@code Throwable} object thrown for
    68      * later retrieval by the {@link #getException()} method. The detail
    69      * later retrieval by the {@link #getException()} method. The detail
    69      * message string is set to <code>null</code>.
    70      * message string is set to {@code null}.
    70      *
    71      *
    71      * @param thrown The exception thrown
    72      * @param thrown The exception thrown
    72      */
    73      */
    73     public ExceptionInInitializerError(Throwable thrown) {
    74     public ExceptionInInitializerError(Throwable thrown) {
    74         super(null, thrown); // Disallow subsequent initCause
    75         super(null, thrown); // Disallow subsequent initCause
    94      * <p>This method predates the general-purpose exception chaining facility.
    95      * <p>This method predates the general-purpose exception chaining facility.
    95      * The {@link Throwable#getCause()} method is now the preferred means of
    96      * The {@link Throwable#getCause()} method is now the preferred means of
    96      * obtaining this information.
    97      * obtaining this information.
    97      *
    98      *
    98      * @return the saved throwable object of this
    99      * @return the saved throwable object of this
    99      *         <code>ExceptionInInitializerError</code>, or <code>null</code>
   100      *         {@code ExceptionInInitializerError}, or {@code null}
   100      *         if this <code>ExceptionInInitializerError</code> has no saved
   101      *         if this {@code ExceptionInInitializerError} has no saved
   101      *         throwable object.
   102      *         throwable object.
   102      */
   103      */
   103     public Throwable getException() {
   104     public Throwable getException() {
   104         return super.getCause();
   105         return super.getCause();
   105     }
   106     }
   107     /**
   108     /**
   108      * Serializable fields for ExceptionInInitializerError.
   109      * Serializable fields for ExceptionInInitializerError.
   109      *
   110      *
   110      * @serialField exception Throwable
   111      * @serialField exception Throwable
   111      */
   112      */
       
   113     @java.io.Serial
   112     private static final ObjectStreamField[] serialPersistentFields = {
   114     private static final ObjectStreamField[] serialPersistentFields = {
   113         new ObjectStreamField("exception", Throwable.class)
   115         new ObjectStreamField("exception", Throwable.class)
   114     };
   116     };
   115 
   117 
   116     /*
   118     /*
   120      *
   122      *
   121      * The getException and getCause method returns the private "exception"
   123      * The getException and getCause method returns the private "exception"
   122      * field in the older implementation and ExceptionInInitializerError::cause
   124      * field in the older implementation and ExceptionInInitializerError::cause
   123      * was set to null.
   125      * was set to null.
   124      */
   126      */
       
   127     @java.io.Serial
   125     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
   128     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
   126         ObjectInputStream.GetField fields = s.readFields();
   129         ObjectInputStream.GetField fields = s.readFields();
   127         Throwable exception = (Throwable) fields.get("exception", null);
   130         Throwable exception = (Throwable) fields.get("exception", null);
   128         if (exception != null) {
   131         if (exception != null) {
   129             setCause(exception);
   132             setCause(exception);
   132 
   135 
   133     /*
   136     /*
   134      * To maintain compatibility with older implementation, write a serial
   137      * To maintain compatibility with older implementation, write a serial
   135      * "exception" field with the cause as the value.
   138      * "exception" field with the cause as the value.
   136      */
   139      */
       
   140     @java.io.Serial
   137     private void writeObject(ObjectOutputStream out) throws IOException {
   141     private void writeObject(ObjectOutputStream out) throws IOException {
   138         ObjectOutputStream.PutField fields = out.putFields();
   142         ObjectOutputStream.PutField fields = out.putFields();
   139         fields.put("exception", super.getCause());
   143         fields.put("exception", super.getCause());
   140         out.writeFields();
   144         out.writeFields();
   141     }
   145     }