Showing posts with label Unchecked Exception. Show all posts
Showing posts with label Unchecked Exception. Show all posts

Difference between ClassNotFoundException and NoClassDefFoundError

When we see in general both ClassNotFoundException and NoClassDefFoundError are errors which comes when JVM or Class Loader not able to find appropriate class while loading at run-time. ClassNotFoundException is a checked exception and NoClassDefFoundError is an Error which comes under unchecked. 
There are different types of Class Loader loads classes from difference sources, sometimes it may cause library JAR files missing or incorrect class-path which causes loader not able to load the class at run-time. Even though both names sounds similar to class file missing there are difference between ClassNotFoundException and NoClassDefFoundError.
Difference between ClassNotFoundException and NoClassDefFoundError

ClassNotFoundException:
ClassNotFoundException comes when we try to load class at run-time using Reflection and if those class files are missing then application or program thrown with ClassNotFoundException Exception. There is nothing to check at compile time since its load those class at run-time. Best example is when we try to load JDBC driver class using reflection and we fail to point driver jars. Then program thrown with ClassNotFoundException as like below example.



import java.sql.Connection;
import java.sql.DriverManager;

public class ExceptionTest {

 public static void main(String[] args) {
  Connection conn = createConnection();
 }

 public static Connection createConnection() {

  Connection conn = null;

  try {
   Class.forName("com.mysql.jdbc.Driver");
   System.out.println("MySQL JDBC Driver Registered!");
   conn = DriverManager.getConnection(
     "jdbc:mysql://192.168.1.2:3306/mydatabase", "root", "root");

  } catch (Exception e) {
   System.err.println("Please configure mysql driver file !!!");
   e.printStackTrace();
  }
  return conn;
 }
}


OUTPUT:


Please configure mysql driver file !!!
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
 at java.net.URLClassLoader$1.run(Unknown Source)


These are the different class loaders from difference classes like,
forName method in class Class.
loadClass method in class ClassLoader.
findSystemClass method in class ClassLoader.


NoClassDefFoundError:

NoClassDefFoundError is thrown when a class has been compiled with a specific class from the class path but if same class not available during run-time. Missing JAR files are the most basic reason to get NoClassDefFoundError. As per Java API docs "The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found."


Lets see simple example to get NoClassDefFoundError Error.


public class SecondTestClass {

 public SecondTestClass(){
  System.out.println("Hello Java");
 }
}



public class ErrorTest {

 public static void main(String[] args) {
  new SecondTestClass();
 } 
}


NOTE:
Once you create both java files compile SecondTestClass.java and ErrorTest.java classes separately. Next delete the generated class file called SecondTestClass.class. Next try to run ErrorTest.class file which will thrown with NoClassDefFoundError error message.

OUTPUT:


Exception in thread "main" java.lang.NoClassDefFoundError: SecondTestClass
 at com.db.ErrorTest.main(ErrorTest.java:7)
Caused by: java.lang.ClassNotFoundException: SecondTestClass







Checked and Unchecked Exceptions in Java


Exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. By using try-catch in a method and if any error occurs then object creates and handed over to the run-time system. Those objects are called as Exception object which contains information's about the exception occurred. 
In our earlier tutorial we have seen difference between throw and throws in java and these exception objects are called as throwing an exception to the run-time system. Normally there are 3 types of Exceptions in Java and they are 

1. Checked exception (or) Compile time exception 
2. Unchecked exception (or) Run-time exception
3. Error 

Below are the Exception hierarchy in Java.


Checked and Unchecked Exceptions in Java


Checked Exception:

Exceptions which are checked under the block or method and caught at compile time are called as checked exceptions. For example if we are reading a file with input as file name and by mistake file name supplied with wrong name then FileNotFoundException will be thrown from the constructor for non-existing file. A well written program will catch those exception and passed to the run-time system. 

Unchecked Exception:

Unchecked Exception are also called as run-time exception which are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These are usually called as programming bugs are API errors which occurs at run-time. For example consider a method passes the file-name to other method. By some programming flaws its started sending null to the method which caught under NullPointerExpcetion.

Errors: 

Errors also called as unchecked exceptions which are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. For example if application successfully opens a input file, but is unable to read the file because of a hardware or system failure are called as Errors which can't be recovered. 

Below are the few list of checked and Unchecked exceptions in java 

Checked Exceptions:

Exception
FileNotFoundException
ParseException
IOException
NoSuchFieldException
NoSuchMethodException
ClassNotFoundException
CloneNotSupportedException
InstantiationException
InterruptedException


Unchecked Exceptions:

NullPointerException
NumberFormatException
ArrayIndexOutOfBoundsException
AssertionError
NoClassDefFoundError
ClassCastException
StackOverflowError
IllegalArgumentException
IllegalStateException
ExceptionInInitializerError