相信java的开发者经常会遇到这两个异常,乍一看两个就长得很像,有时候真的是老虎,老鼠傻傻让人分不清楚。
两者对比
类别
ClassNotFoundException
NoClassDefFoundError
含义
找不到类定义
找不到类定义
继承
extends ReflectiveOperationException
extends LinkageError
类型
反射操作引起的一种异常
一种链接错误
捕获
catch Exception/Throwable
catch Error/Throwable
原因
通过反射根据指定类名无法找到类时,会引起该异常
编译时有类定义,运行时丢失类定义会引起该异常。运行时类定义丢失往往有两种情况:1)类不在ClassPath内,导致无法加载类定义,即由ClassNotFoundException引起。2)类初始化失败,导致加载不到类定义,此时会伴随着一个初始化异常(ExceptionInInitializerError)。
两者关系
NoClassDefFoundError可由ClassNotFoundException引起。
两者定义
当JVM或类加载器去加载一个类定义(无论是通过普通的方法调用,还是用new语句去创建该类的实例所触发的类加载),如果该类的定义丢失,那么就会抛出NoClassDefFoundError. 被加载的类定义在当前执行类编译时是存在的,但是再也找不到了
当应用使用类的字符串名,通过以下方式尝试去加载类,但是根据指定的名称无法找到类定义时会抛出ClassNotFoundException:
1)Class.forName()
2)ClassLoader.findSystemClass()
3)ClassLoader.loadClass()
举例
ClassNotFoundExcption
package qiweiTest;
public class ClassNotFoundExcptionTest {
public static void main(String[] args) {
Class.forName("anyNoexistClassName"); //任何一个不存在的className
}
}
NoClassDefError
(1) 类不在ClassPath引发的异常
package qiweiTest;
public class NoClassDefErrorTest {
//运行时,删除ClassPath中的ClassNotFoundExceptionTest.class
public static void main(String[] args) {
ClassNotFoundExceptionTest.class.getClass();
}
}
输出:
Exception in thread "main" java.lang.NoClassDefFoundError: qiweiTest/ClassNotFoundExceptionTest
at qiweiTest.NoClassDefErrorTest.main(NoClassDefErrorTest.java:9)
Caused by: java.lang.ClassNotFoundException: qiweiTest.ClassNotFoundExceptionTest
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
(2)类初始化失败引发的异常
package qiweiTest;
public class NoClassDefErrorTest2 {
public static void main(String[] args) {
int tmp;
try {
tmp = InterClass.a;
} catch (Throwable e) {
//异常类型:ExceptionInInitializerError
e.printStackTrace();
}
try {
tmp = InterClass.a;
} catch (Throwable e) {
//异常类型:NoClassDefFoundError
e.printStackTrace();
}
}
static class InterClass {
static int a;
static {
a = 1 / 0;
}
}
输出:
java.lang.ExceptionInInitializerError
at qiweiTest.NoClassDefErrorTest2.main(NoClassDefErrorTest2.java:8)
Caused by: java.lang.ArithmeticException: / by zero
at qiweiTest.NoClassDefErrorTest2$InterClass.<clinit>(NoClassDefErrorTest2.java:22)
... 1 more
java.lang.NoClassDefFoundError: Could not initialize class qiweiTest.NoClassDefErrorTest2$InterClass
at qiweiTest.NoClassDefErrorTest2.main(NoClassDefErrorTest2.java:13)
源码
ClassNotFoundException
/**
* Thrown when an application tries to load in a class through its
* string name using:
* <ul>
* <li>The <code>forName</code> method in class <code>Class</code>.
* <li>The <code>findSystemClass</code> method in class
* <code>ClassLoader</code> .
* <li>The <code>loadClass</code> method in class <code>ClassLoader</code>.
* </ul>
* <p>
* but no definition for the class with the specified name could be found.
*
* <p>As of release 1.4, this exception has been retrofitted to conform to
* the general purpose exception-chaining mechanism. The "optional exception
* that was raised while loading the class" that may be provided at
* construction time and accessed via the {@link #getException()} method is
* now known as the <i>cause</i>, and may be accessed via the {@link
* Throwable#getCause()} method, as well as the aforementioned "legacy method."
*
*/
public class ClassNotFoundException extends ReflectiveOperationException {
private static final long serialVersionUID = 9176873029745254542L;
private Throwable ex;
/**
* Constructs a <code>ClassNotFoundException</code> with no detail message.
*/
public ClassNotFoundException() {
super((Throwable)null); // Disallow initCause
}
/**
* Constructs a <code>ClassNotFoundException</code> with the
* specified detail message.
*
* @param s the detail message.
*/
public ClassNotFoundException(String s) {
super(s, null); // Disallow initCause
}
/**
* Constructs a <code>ClassNotFoundException</code> with the
* specified detail message and optional exception that was
* raised while loading the class.
public ClassNotFoundException(String s, Throwable ex) {
super(s, null); // Disallow initCause
this.ex = ex;
}
/**
* Returns the exception that was raised if an error occurred while
* attempting to load the class. Otherwise, returns <tt>null</tt>.
*
* <p>This method predates the general-purpose exception chaining facility.
* The {@link Throwable#getCause()} method is now the preferred means of
* obtaining this information.
*
* @return the <code>Exception</code> that was raised while loading a class
*/
public Throwable getException() {
return ex;
}
/**
* Returns the cause of this exception (the exception that was raised
* if an error occurred while attempting to load the class; otherwise
* <tt>null</tt>).
*
* @return the cause of this exception.
*/
public Throwable getCause() {
return ex;
}
}
NoClassDefFoundError
/**
* Thrown if the Java Virtual Machine or a <code>ClassLoader</code> instance
* tries to load in the definition of a class (as part of a normal method call
* or as part of creating a new instance using the <code>new</code> expression)
* and no definition of the class could be found.
* <p>
* The searched-for class definition existed when the currently
* executing class was compiled, but the definition can no longer be
* found.
*/
public class NoClassDefFoundError extends LinkageError {
private static final long serialVersionUID = 9095859863287012458L;
/**
* Constructs a <code>NoClassDefFoundError</code> with no detail message.
*/
public NoClassDefFoundError() {
super();
}
/**
* Constructs a <code>NoClassDefFoundError</code> with the specified
* detail message.
*
* @param s the detail message.
*/
public NoClassDefFoundError(String s) {
super(s);
}
}
作者:王奇伟
链接:juejin.cn/post/684490…
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。