< prev index next >

## src/java.base/share/classes/java/lang/ClassNotFoundException.java

Print this page

      1 /*
      2  * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
      3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      4  *
      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
      7  * published by the Free Software Foundation.  Oracle designates this
      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
     93     }
     94
     95     /**
     96      * Returns the exception that was raised if an error occurred while
     97      * attempting to load the class. Otherwise, returns {@code null}.
     98      *
     99      * <p>This method predates the general-purpose exception chaining facility.
    100      * The {@link Throwable#getCause()} method is now the preferred means of
    101      * obtaining this information.
    102      *
    103      * @return the {@code Exception} that was raised while loading a class
    104      * @since 1.2
    105      */
    106     public Throwable getException() {
    107         return super.getCause();
    108     }
    109
    110     /**
    111      * Serializable fields for ClassNotFoundException.
    112      *
    113      * @serialField ex Throwable
    114      */
    115     @java.io.Serial
    116     private static final ObjectStreamField[] serialPersistentFields = {
    117         new ObjectStreamField("ex", Throwable.class)
    118     };
    119
    120     /*
    121      * Reconstitutes the ClassNotFoundException instance from a stream
    122      * and initialize the cause properly when deserializing from an older
    123      * version.
    124      *
    125      * The getException and getCause method returns the private "ex" field
    126      * in the older implementation and ClassNotFoundException::cause
    127      * was set to null.

    128      */
    129     @java.io.Serial
    130     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
    131         ObjectInputStream.GetField fields = s.readFields();
    132         Throwable exception = (Throwable) fields.get("ex", null);
    133         if (exception != null) {
    134             setCause(exception);
    135         }
    136     }
    137
    138     /*
    139      * To maintain compatibility with older implementation, write a serial
    140      * "ex" field with the cause as the value.

    141      */
    142     @java.io.Serial
    143     private void writeObject(ObjectOutputStream out) throws IOException {
    144         ObjectOutputStream.PutField fields = out.putFields();
    145         fields.put("ex", super.getCause());
    146         out.writeFields();
    147     }
    148 }
      1 /*
      2  * Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
      3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      4  *
      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
      7  * published by the Free Software Foundation.  Oracle designates this
      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
     93     }
     94
     95     /**
     96      * Returns the exception that was raised if an error occurred while
     97      * attempting to load the class. Otherwise, returns {@code null}.
     98      *
     99      * <p>This method predates the general-purpose exception chaining facility.
    100      * The {@link Throwable#getCause()} method is now the preferred means of
    101      * obtaining this information.
    102      *
    103      * @return the {@code Exception} that was raised while loading a class
    104      * @since 1.2
    105      */
    106     public Throwable getException() {
    107         return super.getCause();
    108     }
    109
    110     /**
    111      * Serializable fields for ClassNotFoundException.
    112      *
    113      * @serialField ex Throwable  the {@code Throwable}
    114      */
    115     @java.io.Serial
    116     private static final ObjectStreamField[] serialPersistentFields = {
    117         new ObjectStreamField("ex", Throwable.class)
    118     };
    119
    120     /**
    121      * Reconstitutes the ClassNotFoundException instance from a stream
    122      * and initialize the cause properly when deserializing from an older
    123      * version.
    124      *
    125      * The getException and getCause method returns the private "ex" field
    126      * in the older implementation and ClassNotFoundException::cause
    127      * was set to null.
    128      *
    129      * @param  s the {@code ObjectInputStream} from which data is read
    130      * @throws IOException if an I/O error occurs
    131      * @throws ClassNotFoundException if a serialized class cannot be loaded
    132      */
    133     @java.io.Serial
    134     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
    135         ObjectInputStream.GetField fields = s.readFields();
    136         Throwable exception = (Throwable) fields.get("ex", null);
    137         if (exception != null) {
    138             setCause(exception);
    139         }
    140     }
    141
    142     /**
    143      * To maintain compatibility with older implementation, write a serial
    144      * "ex" field with the cause as the value.
    145      *
    146      * @param  out the {@code ObjectOutputStream} to which data is written
    147      * @throws IOException if an I/O error occurs
    148      */
    149     @java.io.Serial
    150     private void writeObject(ObjectOutputStream out) throws IOException {
    151         ObjectOutputStream.PutField fields = out.putFields();
    152         fields.put("ex", super.getCause());
    153         out.writeFields();
    154     }
    155 }

< prev index next >