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 /**
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 }
|
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 @java.io.Serial
63 private static final long serialVersionUID = 9176873029745254542L;
64
65 /**
66 * Constructs a <code>ClassNotFoundException</code> with no detail message.
67 */
68 public ClassNotFoundException() {
69 super((Throwable)null); // Disallow initCause
70 }
71
72 /**
73 * Constructs a <code>ClassNotFoundException</code> with the
74 * specified detail message.
75 *
76 * @param s the detail message.
77 */
78 public ClassNotFoundException(String s) {
79 super(s, null); // Disallow initCause
80 }
81
82 /**
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</code> 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 }
|