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 * Thrown when an application tries to load in a class through its 30 * string name using: 31 * <ul> 32 * <li>The <code>forName</code> method in class <code>Class</code>. 33 * <li>The <code>findSystemClass</code> method in class 34 * <code>ClassLoader</code> . 35 * <li>The <code>loadClass</code> method in class <code>ClassLoader</code>. 36 * </ul> 37 * <p> 38 * but no definition for the class with the specified name could be found. 39 * 40 * <p>As of release 1.4, this exception has been retrofitted to conform to 41 * the general purpose exception-chaining mechanism. The "optional exception 42 * that was raised while loading the class" that may be provided at 43 * construction time and accessed via the {@link #getException()} method is 44 * now known as the <i>cause</i>, and may be accessed via the {@link 45 * Throwable#getCause()} method, as well as the aforementioned "legacy method." 46 * 47 * @author unascribed 48 * @see java.lang.Class#forName(java.lang.String) 49 * @see java.lang.ClassLoader#findSystemClass(java.lang.String) 50 * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean) 51 * @since 1.0 52 */ 53 public class ClassNotFoundException extends ReflectiveOperationException { 54 /** 55 * use serialVersionUID from JDK 1.1.X for interoperability 56 */ 57 private static final long serialVersionUID = 9176873029745254542L; 58 59 /** 60 * This field holds the exception ex if the 61 * ClassNotFoundException(String s, Throwable ex) constructor was 62 * used to instantiate the object 63 * @serial 64 * @since 1.2 65 */ 66 private Throwable ex; 67 68 /** 69 * Constructs a <code>ClassNotFoundException</code> with no detail message. 70 */ 71 public ClassNotFoundException() { 72 super((Throwable)null); // Disallow initCause 73 } 74 75 /** 76 * Constructs a <code>ClassNotFoundException</code> with the 77 * specified detail message. 78 * 79 * @param s the detail message. 80 */ 81 public ClassNotFoundException(String s) { 82 super(s, null); // Disallow initCause 83 } 84 85 /** 86 * Constructs a <code>ClassNotFoundException</code> with the 87 * specified detail message and optional exception that was 88 * raised while loading the class. 89 * 90 * @param s the detail message 91 * @param ex the exception that was raised while loading the class 92 * @since 1.2 93 */ 94 public ClassNotFoundException(String s, Throwable ex) { 95 super(s, null); // Disallow initCause 96 this.ex = ex; 97 } 98 99 /** 100 * Returns the exception that was raised if an error occurred while 101 * attempting to load the class. Otherwise, returns {@code null}. 102 * 103 * <p>This method predates the general-purpose exception chaining facility. 104 * The {@link Throwable#getCause()} method is now the preferred means of 105 * obtaining this information. 106 * 107 * @return the <code>Exception</code> that was raised while loading a class 108 * @since 1.2 109 */ 110 public Throwable getException() { 111 return ex; 112 } 113 114 /** 115 * Returns the cause of this exception (the exception that was raised 116 * if an error occurred while attempting to load the class; otherwise 117 * {@code null}). 118 * 119 * @return the cause of this exception. 120 * @since 1.4 121 */ 122 public Throwable getCause() { 123 return ex; 124 } 125 } | 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 * Thrown when an application tries to load in a class through its 35 * string name using: 36 * <ul> 37 * <li>The <code>forName</code> method in class <code>Class</code>. 38 * <li>The <code>findSystemClass</code> method in class 39 * <code>ClassLoader</code> . 40 * <li>The <code>loadClass</code> method in class <code>ClassLoader</code>. 41 * </ul> 42 * <p> 43 * but no definition for the class with the specified name could be found. 44 * 45 * <p>As of release 1.4, this exception has been retrofitted to conform to 46 * the general purpose exception-chaining mechanism. The "optional exception 47 * that was raised while loading the class" that may be provided at 48 * construction time and accessed via the {@link #getException()} method is 49 * now known as the <i>cause</i>, and may be accessed via the {@link 50 * Throwable#getCause()} method, as well as the aforementioned "legacy method." 51 * 52 * @author unascribed 53 * @see java.lang.Class#forName(java.lang.String) 54 * @see java.lang.ClassLoader#findSystemClass(java.lang.String) 55 * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean) 56 * @since 1.0 57 */ 58 public class ClassNotFoundException extends ReflectiveOperationException { 59 /** 60 * use serialVersionUID from JDK 1.1.X for interoperability 61 */ 62 private static final long serialVersionUID = 9176873029745254542L; 63 64 /** 65 * Constructs a <code>ClassNotFoundException</code> with no detail message. 66 */ 67 public ClassNotFoundException() { 68 super((Throwable)null); // Disallow initCause 69 } 70 71 /** 72 * Constructs a <code>ClassNotFoundException</code> with the 73 * specified detail message. 74 * 75 * @param s the detail message. 76 */ 77 public ClassNotFoundException(String s) { 78 super(s, null); // Disallow initCause 79 } 80 81 /** 82 * Constructs a <code>ClassNotFoundException</code> with the 83 * specified detail message and optional exception that was 84 * raised while loading the class. 85 * 86 * @param s the detail message 87 * @param ex the exception that was raised while loading the class 88 * @since 1.2 89 */ 90 public ClassNotFoundException(String s, Throwable ex) { 91 super(s, ex); // Disallow initCause 92 } 93 94 /** 95 * Returns the exception that was raised if an error occurred while 96 * attempting to load the class. Otherwise, returns {@code null}. 97 * 98 * <p>This method predates the general-purpose exception chaining facility. 99 * The {@link Throwable#getCause()} method is now the preferred means of 100 * obtaining this information. 101 * 102 * @return the <code>Exception</code> that was raised while loading a class 103 * @since 1.2 104 */ 105 public Throwable getException() { 106 return super.getCause(); 107 } 108 109 /** 110 * Serializable fields for ClassNotFoundException. 111 * 112 * @serialField ex Throwable 113 */ 114 private static final ObjectStreamField[] serialPersistentFields = { 115 new ObjectStreamField("ex", Throwable.class) 116 }; 117 118 /* 119 * Reconstitutes the ClassNotFoundException instance from a stream 120 * and initialize the cause properly when deserializing from an older 121 * version. 122 * 123 * The getException and getCause method returns the private "ex" field 124 * in the older implementation and ClassNotFoundException::cause 125 * was set to null. 126 */ 127 private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { 128 ObjectInputStream.GetField fields = s.readFields(); 129 Throwable exception = (Throwable) fields.get("ex", null); 130 if (exception != null) { 131 setCause(exception); 132 } 133 } 134 135 /* 136 * To maintain compatibility with older implementation, write a serial 137 * "ex" field with the cause as the value. 138 */ 139 private void writeObject(ObjectOutputStream out) throws IOException { 140 ObjectOutputStream.PutField fields = out.putFields(); 141 fields.put("ex", super.getCause()); 142 out.writeFields(); 143 } 144 } |