8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang; 27 28 /** 29 * Signals that an unexpected exception has occurred in a static initializer. 30 * An <code>ExceptionInInitializerError</code> is thrown to indicate that an 31 * exception occurred during evaluation of a static initializer or the 32 * initializer for a static variable. 33 * 34 * <p>As of release 1.4, this exception has been retrofitted to conform to 35 * the general purpose exception-chaining mechanism. The "saved throwable 36 * object" that may be provided at construction time and accessed via 37 * the {@link #getException()} method is now known as the <i>cause</i>, 38 * and may be accessed via the {@link Throwable#getCause()} method, as well 39 * as the aforementioned "legacy method." 40 * 41 * @author Frank Yellin 42 * @since 1.1 43 */ 44 public class ExceptionInInitializerError extends LinkageError { 45 /** 46 * Use serialVersionUID from JDK 1.1.X for interoperability 47 */ 48 private static final long serialVersionUID = 1521711792217232256L; 49 50 /** 51 * This field holds the exception if the 52 * ExceptionInInitializerError(Throwable thrown) constructor was 53 * used to instantiate the object 54 * 55 * @serial 56 * 57 */ 58 private Throwable exception; 59 60 /** 61 * Constructs an <code>ExceptionInInitializerError</code> with 62 * <code>null</code> as its detail message string and with no saved 63 * throwable object. 64 * A detail message is a String that describes this particular exception. 65 */ 66 public ExceptionInInitializerError() { 67 initCause(null); // Disallow subsequent initCause 68 } 69 70 /** 71 * Constructs a new <code>ExceptionInInitializerError</code> class by 72 * saving a reference to the <code>Throwable</code> object thrown for 73 * later retrieval by the {@link #getException()} method. The detail 74 * message string is set to <code>null</code>. 75 * 76 * @param thrown The exception thrown 77 */ 78 public ExceptionInInitializerError(Throwable thrown) { 79 initCause(null); // Disallow subsequent initCause 80 this.exception = thrown; 81 } 82 83 /** 84 * Constructs an ExceptionInInitializerError with the specified detail 85 * message string. A detail message is a String that describes this 86 * particular exception. The detail message string is saved for later 87 * retrieval by the {@link Throwable#getMessage()} method. There is no 88 * saved throwable object. 89 * 90 * 91 * @param s the detail message 92 */ 93 public ExceptionInInitializerError(String s) { 94 super(s); 95 initCause(null); // Disallow subsequent initCause 96 } 97 98 /** 99 * Returns the exception that occurred during a static initialization that 100 * caused this error to be created. 101 * 102 * <p>This method predates the general-purpose exception chaining facility. 103 * The {@link Throwable#getCause()} method is now the preferred means of 104 * obtaining this information. 105 * 106 * @return the saved throwable object of this 107 * <code>ExceptionInInitializerError</code>, or <code>null</code> 108 * if this <code>ExceptionInInitializerError</code> has no saved 109 * throwable object. 110 */ 111 public Throwable getException() { 112 return exception; 113 } 114 115 /** 116 * Returns the cause of this error (the exception that occurred 117 * during a static initialization that caused this error to be created). 118 * 119 * @return the cause of this error or <code>null</code> if the 120 * cause is nonexistent or unknown. 121 * @since 1.4 122 */ 123 public Throwable getCause() { 124 return exception; 125 } 126 } | 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang; 27 28 import java.io.IOException; 29 import java.io.ObjectInputStream; 30 import java.io.ObjectOutputStream; 31 import java.io.ObjectStreamField; 32 33 /** 34 * Signals that an unexpected exception has occurred in a static initializer. 35 * An <code>ExceptionInInitializerError</code> is thrown to indicate that an 36 * exception occurred during evaluation of a static initializer or the 37 * initializer for a static variable. 38 * 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 41 * object" that may be provided at construction time and accessed via 42 * the {@link #getException()} method is now known as the <i>cause</i>, 43 * and may be accessed via the {@link Throwable#getCause()} method, as well 44 * as the aforementioned "legacy method." 45 * 46 * @author Frank Yellin 47 * @since 1.1 48 */ 49 public class ExceptionInInitializerError extends LinkageError { 50 /** 51 * Use serialVersionUID from JDK 1.1.X for interoperability 52 */ 53 private static final long serialVersionUID = 1521711792217232256L; 54 55 /** 56 * Constructs an <code>ExceptionInInitializerError</code> with 57 * <code>null</code> as its detail message string and with no saved 58 * throwable object. 59 * A detail message is a String that describes this particular exception. 60 */ 61 public ExceptionInInitializerError() { 62 initCause(null); // Disallow subsequent initCause 63 } 64 65 /** 66 * Constructs a new <code>ExceptionInInitializerError</code> class by 67 * saving a reference to the <code>Throwable</code> object thrown for 68 * later retrieval by the {@link #getException()} method. The detail 69 * message string is set to <code>null</code>. 70 * 71 * @param thrown The exception thrown 72 */ 73 public ExceptionInInitializerError(Throwable thrown) { 74 super(null, thrown); ; // Disallow subsequent initCause 75 } 76 77 /** 78 * Constructs an {@code ExceptionInInitializerError} with the specified detail 79 * message string. A detail message is a String that describes this 80 * particular exception. The detail message string is saved for later 81 * retrieval by the {@link Throwable#getMessage()} method. There is no 82 * saved throwable object. 83 * 84 * @param s the detail message 85 */ 86 public ExceptionInInitializerError(String s) { 87 super(s, null); // Disallow subsequent initCause 88 } 89 90 /** 91 * Returns the exception that occurred during a static initialization that 92 * caused this error to be created. 93 * 94 * <p>This method predates the general-purpose exception chaining facility. 95 * The {@link Throwable#getCause()} method is now the preferred means of 96 * obtaining this information. 97 * 98 * @return the saved throwable object of this 99 * <code>ExceptionInInitializerError</code>, or <code>null</code> 100 * if this <code>ExceptionInInitializerError</code> has no saved 101 * throwable object. 102 */ 103 public Throwable getException() { 104 return getCause(); 105 } 106 107 /** 108 * Serializable fields for ExceptionInInitializerError. 109 * 110 * @serialField exception Throwable 111 */ 112 private static final ObjectStreamField[] serialPersistentFields = { 113 new ObjectStreamField("exception", Throwable.class) 114 }; 115 116 /* 117 * Reconstitutes the ExceptionInInitializerError instance from a stream 118 * and initialize the cause properly when deserializing from an older 119 * version. 120 * 121 * The getException and getCause method returns the private "exception" 122 * field in the older implementation and ExceptionInInitializerError::cause 123 * was set to null. 124 */ 125 private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { 126 ObjectInputStream.GetField fields = s.readFields(); 127 Throwable exception = (Throwable) fields.get("exception", null); 128 if (exception != null) { 129 setCause(exception); 130 } 131 } 132 133 /* 134 * To maintain compatibility with older implementation, write a serial 135 * "exception" field with the cause as the value. 136 */ 137 private void writeObject(ObjectOutputStream out) throws IOException { 138 ObjectOutputStream.PutField fields = out.putFields(); 139 fields.put("exception", getCause()); 140 out.writeFields(); 141 } 142 143 } |