Path: blob/master/src/java.base/share/classes/java/lang/ExceptionInInitializerError.java
41152 views
/*1* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;2627import java.io.IOException;28import java.io.ObjectInputStream;29import java.io.ObjectOutputStream;30import java.io.ObjectStreamField;3132/**33* Signals that an unexpected exception has occurred in a static initializer.34* An {@code ExceptionInInitializerError} is thrown to indicate that an35* exception occurred during evaluation of a static initializer or the36* initializer for a static variable.37*38* @author Frank Yellin39* @since 1.140*/41public class ExceptionInInitializerError extends LinkageError {42/**43* Use serialVersionUID from JDK 1.1.X for interoperability44*/45@java.io.Serial46private static final long serialVersionUID = 1521711792217232256L;4748/**49* Constructs an {@code ExceptionInInitializerError} with50* {@code null} as its detail message string and with no saved51* throwable object.52* A detail message is a String that describes this particular exception.53*/54public ExceptionInInitializerError() {55initCause(null); // Disallow subsequent initCause56}5758/**59* Constructs a new {@code ExceptionInInitializerError} class by60* saving a reference to the {@code Throwable} object thrown for61* later retrieval by the {@link #getException()} method. The detail62* message string is set to {@code null}.63*64* @param thrown The exception thrown65*/66public ExceptionInInitializerError(Throwable thrown) {67super(null, thrown); // Disallow subsequent initCause68}6970/**71* Constructs an {@code ExceptionInInitializerError} with the specified detail72* message string. A detail message is a String that describes this73* particular exception. The detail message string is saved for later74* retrieval by the {@link Throwable#getMessage()} method. There is no75* saved throwable object.76*77* @param s the detail message78*/79public ExceptionInInitializerError(String s) {80super(s, null); // Disallow subsequent initCause81}8283/**84* Returns the exception that occurred during a static initialization that85* caused this error to be created.86*87* @apiNote88* This method predates the general-purpose exception chaining facility.89* The {@link Throwable#getCause()} method is now the preferred means of90* obtaining this information.91*92* @return the saved throwable object of this93* {@code ExceptionInInitializerError}, or {@code null}94* if this {@code ExceptionInInitializerError} has no saved95* throwable object.96*/97public Throwable getException() {98return super.getCause();99}100101/**102* Serializable fields for ExceptionInInitializerError.103*104* @serialField exception Throwable the exception105*/106@java.io.Serial107private static final ObjectStreamField[] serialPersistentFields = {108new ObjectStreamField("exception", Throwable.class)109};110111/**112* Reconstitutes the ExceptionInInitializerError instance from a stream113* and initialize the cause properly when deserializing from an older114* version.115*116* The getException and getCause method returns the private "exception"117* field in the older implementation and ExceptionInInitializerError::cause118* was set to null.119*120* @param s the {@code ObjectInputStream} from which data is read121* @throws IOException if an I/O error occurs122* @throws ClassNotFoundException if a serialized class cannot be loaded123*/124@java.io.Serial125private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {126ObjectInputStream.GetField fields = s.readFields();127Throwable exception = (Throwable) fields.get("exception", null);128if (exception != null) {129setCause(exception);130}131}132133/**134* To maintain compatibility with older implementation, write a serial135* "exception" field with the cause as the value.136*137* @param out the {@code ObjectOutputStream} to which data is written138* @throws IOException if an I/O error occurs139*/140@java.io.Serial141private void writeObject(ObjectOutputStream out) throws IOException {142ObjectOutputStream.PutField fields = out.putFields();143fields.put("exception", super.getCause());144out.writeFields();145}146147}148149150