我们先来认识一下Error 和Exception, 两个都是Throwable类的直接子类。 Javadoc 很好的说明了Error类:
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.
抛出Error时,程序就是出现了严重的错误,程序会立即终止运行。我们来看一下Error的一些子类和它们在javadoc中的说明:
AnnotationFormatError
- Thrown when the annotation
parser attempts to read an annotation from a class file and determines that
the annotation is malformed.AssertionError
- Thrown to indicate that an assertion
has failed.LinkageError
- Subclasses of LinkageError indicate that
a class has some dependency on another class; however, the latter class has
incompatibly changed after the compilation of the former class.VirtualMachineError
- Thrown to indicate that the Java
Virtual Machine is broken or has run out of resources necessary for it to
continue operating.Throwable的子类 主要分三种类别
:
Error
- 一些严重问题导致的,大多数程序此时应该crash
而不是试图去handle的错误RuntimeException
) -
经常出现的编程错误比如 NullPointerException
或者非法参数.
这个Throwable类别中,程序有时可以处理该种错误 或者 从中恢复-- 或者 至少可以catch
Thread的run方法,记录相应日志,并继续执行。
有了上述基本认识以后,我们回到正题~
在辨析两者的不同前,让我们先看看两者有什么相同点:
现在看看有什么不同:
可能造成 NoClassDefFoundError 的原因有很多:
举个我自己的栗子:
#!/bin/bash BIN_DIR=../MatchSnapshots/bin export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATH java -cp $BIN_DIR:$JAVA_HOME/lib/tools.jar\ edu.umd.MatchSnapshots.Main some other options
源码是使用了com.sun.tools.hat.internal.parser.Reader类,在Eclipse中buildpath里添加了tools.jar以后编译通过并可以运行。在command line下,却抛出了NoClassDefFoundError,使用-cp选项添加了tools.jar就没这个问题了。
参考文章:
Difference between ClassNotFoundException vs NoClassDefFoundError in Java
How
to resolve java.lang.ClassNotFoundException in Java
Differences betweeen Exception and Error
NoClassDefFoundError vs ClassNotFoundException,布布扣,bubuko.com
NoClassDefFoundError vs ClassNotFoundException
原文:http://www.cnblogs.com/ridox/p/3624309.html