1 /* 2 * @(#)ClassNotFoundException.java 1.20 04/02/19 3 * 4 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 5 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 6 */ 7 8 package java.lang; 9 10 /** Thrown when an application tries to load in a class through its string name using: 11 * <ul> 12 * <li>The <code>forName</code> method in class <code>Class</code>. 13 * <li>The <code>findSystemClass</code> method in class <code>ClassLoader</code> . 14 * <li>The <code>loadClass</code> method in class <code>ClassLoader</code>. 15 * </ul> 16 * <p> 17 * but no definition for the class with the specified name could be found. 18 * 19 * <p> 20 * As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The 21 * "optional exception that was raised while loading the class" that may be provided at construction time and accessed via the 22 * {@link #getException()} method is now known as the <i>cause</i>, and may be accessed via the {@link Throwable#getCause()} 23 * method, as well as the aforementioned "legacy method." 24 * 25 * @author unascribed 26 * @version 1.20, 02/19/04 27 * @see java.lang.Class#forName(java.lang.String) 28 * @see java.lang.ClassLoader#findSystemClass(java.lang.String) 29 * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean) 30 * @since JDK1.0 */ 31 public class ClassNotFoundException extends Exception { 32 /** use serialVersionUID from JDK 1.1.X for interoperability */ 33 private static final long serialVersionUID = 9176873029745254542L; 34 35 /** This field holds the exception ex if the ClassNotFoundException(String s, Throwable ex) constructor was used to instantiate 36 * the object 37 * @serial 38 * @since 1.2 */ 39 private Throwable ex; 40 41 /** Constructs a <code>ClassNotFoundException</code> with no detail message. */ ClassNotFoundException()42 public ClassNotFoundException () { 43 super((Throwable)null); // Disallow initCause 44 } 45 46 /** Constructs a <code>ClassNotFoundException</code> with the specified detail message. 47 * 48 * @param s the detail message. */ ClassNotFoundException(String s)49 public ClassNotFoundException (String s) { 50 super(s, null); // Disallow initCause 51 } 52 53 /** Constructs a <code>ClassNotFoundException</code> with the specified detail message and optional exception that was raised 54 * while loading the class. 55 * 56 * @param s the detail message 57 * @param ex the exception that was raised while loading the class 58 * @since 1.2 */ ClassNotFoundException(String s, Throwable ex)59 public ClassNotFoundException (String s, Throwable ex) { 60 super(s, null); // Disallow initCause 61 this.ex = ex; 62 } 63 64 /** Returns the exception that was raised if an error occurred while attempting to load the class. Otherwise, returns 65 * <tt>null</tt>. 66 * 67 * <p> 68 * This method predates the general-purpose exception chaining facility. The {@link Throwable#getCause()} method is now the 69 * preferred means of obtaining this information. 70 * 71 * @return the <code>Exception</code> that was raised while loading a class 72 * @since 1.2 */ getException()73 public Throwable getException () { 74 return ex; 75 } 76 77 /** Returns the cause of this exception (the exception that was raised if an error occurred while attempting to load the class; 78 * otherwise <tt>null</tt>). 79 * 80 * @return the cause of this exception. 81 * @since 1.4 */ getCause()82 public Throwable getCause () { 83 return ex; 84 } 85 } 86