Difference between NoClassDefFoundError and ClassNotFoundException

NoClassDefFoundError

NoClassDefFoundError is child class of the LinkageError and it is unchecked exception.


public class NoClassDefFoundError extends LinkageError


Whenever JVM unable to find the required .class file then JVM will throw NoClassDefFoundError at runtime.

Hierarchy of NoClassDefFoundError


Object => Throwable => Error => LinkageError => NoClassDefFoundError

Example:



public class Test { 
       public static void main(String[] args) {
              System.out.println("Hello world");
       }     
}


Whenever we compile this Test.java source file, it generates the Test.class file. If JVM unable to find the Test.class file while running the Test.class file then we will get NoClassDefFoundError.

Until Java 1.6 version it gives error as

Exception in thread "main" java.lang.NoClassDefFoundError: Test

Java 1.7 version on words, it gives error as

Error: Could not find or load main class Test



ClassNotFoundException


ClassNotFoundException is a child class of ReflectiveOperationException and it is checked exception.


public class ClassNotFoundException extends ReflectiveOperationException


The ClassNotFoundException thrown whenever we try to load a particular class then that class is not found in the classpath.

Hierarchy of ClassNotFoundException


Object => Throwable => Exception => ReflectiveOperationException => ClassNotFoundException

Example:


In this example, I tried to load MySQL driver class but compiler notified to the programmer that there may be a chance to occur ClassNotFoundException.


package com.exceptions;
public class Test{  
       public static void main(String[] args) {
              Class.forName("com.mysql.jdbc.Driver");
       }     
}


The compiler won’t compile the program and gives the error


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
       Unhandled exception type ClassNotFoundException
       at com.core.Test.main(Test.java:4)


The ClassNotFoundException is checked by the compiler and we must declare ClassNotFoundException by using throws clause or handle by using try catch blocks otherwise compiler won’t compile the program.


package com.exceptions;
public class Test{  
       public static void main(String[] args) throws ClassNotFoundException {
              Class.forName("com.mysql.jdbc.Driver");
       }     
}


Now the compiler compiles fine because we declared exception using throws clause, but JVM thrown an exception because we didn’t load any MySQL jar file.

JVM thrown exception as


Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
       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)
       at java.lang.Class.forName0(Native Method)
       at java.lang.Class.forName(Unknown Source)
       at com.core.Test.main(Test.java:4)




No comments:

Post a Comment