Thread: JBPatch
View Single Post
Old 10-26-2012, 07:22 AM   #871
bhaak
Groupie
bhaak can program the VCR without an owner's manual.bhaak can program the VCR without an owner's manual.bhaak can program the VCR without an owner's manual.bhaak can program the VCR without an owner's manual.bhaak can program the VCR without an owner's manual.bhaak can program the VCR without an owner's manual.bhaak can program the VCR without an owner's manual.bhaak can program the VCR without an owner's manual.bhaak can program the VCR without an owner's manual.bhaak can program the VCR without an owner's manual.bhaak can program the VCR without an owner's manual.
 
bhaak's Avatar
 
Posts: 164
Karma: 164969
Join Date: Dec 2011
Device: Palm IIIx, (iPhone|Kindle) Touch
Quote:
Originally Posted by ixtab View Post
Answering my own question: it's probably due to an idiosyncrasy of the JVM, namely the difference between checked and unchecked exceptions. In short: trying to load a named class can throw a (checked=must be caught) "ClassNotFoundException". But if that class is actually found, but isn't "initializable" because it itself depends on another non-found class, that produces an unchecked exception, namely "NoClassDefFoundError". Go figure.
You are correct but it's not the full picture.

It is correct that ClassNotFoundException is a checked Exception and NoClassDefFoundError is an unchecked one:
http://docs.oracle.com/javase/6/docs...Exception.html
http://docs.oracle.com/javase/6/docs...oundError.html

The reason for this is that normally, you don't expect NoClassDefFoundError to be thrown as this is a serious, unexpected fatal error. For example it will be thrown when your code compiled okay but while running, some of the previously compiled classes are missing.

If you compile and run the following code with "javac TestE.java && java TestE" everything is fine.
Code:
public class TestE {
        public static void main (String[] args) {
                System.out.println("Hello World!");
                TestKlass test = new TestKlass();
        }
}
class TestKlass {}
If you try "javac TestE.java; rm -f TestKlass.class; java TestE" you get an NoClassDefFoundError exception as the JVM completely freaked out because of not finding a class that should have been there.

OTOH you get an checked ClassNotFoundException if you dynamically try to load a class (e.g. Class.forName("doesntexist")) as the compiler can't check during compilation if this class does exist and there this is a expected exception to occur.
bhaak is offline   Reply With Quote