Path: blob/master/src/java.base/share/classes/java/lang/ClassNotFoundException.java
41152 views
/*1* Copyright (c) 1995, 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* Thrown when an application tries to load in a class through its34* string name using:35* <ul>36* <li>The {@code forName} method in class {@code Class}.37* <li>The {@code findSystemClass} method in class38* {@code ClassLoader} .39* <li>The {@code loadClass} method in class {@code ClassLoader}.40* </ul>41* <p>42* but no definition for the class with the specified name could be found.43*44* @see java.lang.Class#forName(java.lang.String)45* @see java.lang.ClassLoader#findSystemClass(java.lang.String)46* @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)47* @since 1.048*/49public class ClassNotFoundException extends ReflectiveOperationException {50/**51* use serialVersionUID from JDK 1.1.X for interoperability52*/53@java.io.Serial54private static final long serialVersionUID = 9176873029745254542L;5556/**57* Constructs a {@code ClassNotFoundException} with no detail message.58*/59public ClassNotFoundException() {60super((Throwable)null); // Disallow initCause61}6263/**64* Constructs a {@code ClassNotFoundException} with the65* specified detail message.66*67* @param s the detail message.68*/69public ClassNotFoundException(String s) {70super(s, null); // Disallow initCause71}7273/**74* Constructs a {@code ClassNotFoundException} with the75* specified detail message and optional exception that was76* raised while loading the class.77*78* @param s the detail message79* @param ex the exception that was raised while loading the class80* @since 1.281*/82public ClassNotFoundException(String s, Throwable ex) {83super(s, ex); // Disallow initCause84}8586/**87* Returns the exception that was raised if an error occurred while88* attempting to load the class. Otherwise, returns {@code null}.89*90* @apiNote91* This method predates the general-purpose exception chaining facility.92* The {@link Throwable#getCause()} method is now the preferred means of93* obtaining this information.94*95* @return the {@code Exception} that was raised while loading a class96* @since 1.297*/98public Throwable getException() {99return super.getCause();100}101102/**103* Serializable fields for ClassNotFoundException.104*105* @serialField ex Throwable the {@code Throwable}106*/107@java.io.Serial108private static final ObjectStreamField[] serialPersistentFields = {109new ObjectStreamField("ex", Throwable.class)110};111112/**113* Reconstitutes the ClassNotFoundException instance from a stream114* and initialize the cause properly when deserializing from an older115* version.116*117* The getException and getCause method returns the private "ex" field118* in the older implementation and ClassNotFoundException::cause119* was set to null.120*121* @param s the {@code ObjectInputStream} from which data is read122* @throws IOException if an I/O error occurs123* @throws ClassNotFoundException if a serialized class cannot be loaded124*/125@java.io.Serial126private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {127ObjectInputStream.GetField fields = s.readFields();128Throwable exception = (Throwable) fields.get("ex", null);129if (exception != null) {130setCause(exception);131}132}133134/**135* To maintain compatibility with older implementation, write a serial136* "ex" field with the cause as the value.137*138* @param out the {@code ObjectOutputStream} to which data is written139* @throws IOException if an I/O error occurs140*/141@java.io.Serial142private void writeObject(ObjectOutputStream out) throws IOException {143ObjectOutputStream.PutField fields = out.putFields();144fields.put("ex", super.getCause());145out.writeFields();146}147}148149150