Difference between Checked Exception and Unchecked Exception

Hierarchy of Java exception class


The Throwable class is the root class of Java exception hierarchy, it contains two child classes.

=> Exception
=> Error



Note:  we have so many different types of exceptions and errors, not only shown in the diagram.

Exception:

Exceptions are occurring by the programmer during runtime and it is recoverable.

E.g: ArithmeticException, it will occur when we divide any number by zero

There are mainly two types of exceptions: 


checked exceptions and unchecked exceptions.

Checked Exception:


Checked exception classes which extend Throwable class except RuntimeException and Error.

Checked exceptions are checked by the compiler during compilation for smooth execution of the program at runtime.

e.g: SQLException, FileNotFoundException, ClassNotFoundException, InterruptedException etc.

Example:



package com.exceptions;
import java.io.PrintWriter;
public class FileNotFoundTest {  
       public static void main(String[] args) {       
              PrintWriter pw = new PrintWriter("abc.txt");          
                     pw.println("hello world");
                     pw.close();         
       }
}


Output:



Exception in thread "main" java.lang.Error: Unresolved compilation problem:
       Unhandled exception type FileNotFoundException
       at com.exceptions.FileNotFoundTest.main(FileNotFoundTest.java:5)



Checked exceptions don’t occur during compile time but checked by the compiler during compilation and notify to the developer that there could be an exception occurs at runtime.

Unchecked Exception:


Unchecked exception classes which extend RuntimeException class and unchecked exceptions checked at runtime.

e.g: ClassCastException, ArithmeticException, NullPointerException, NumberFormat Exception, ArrayIndexOutOfBoundException and etc.

Note: checked exceptions and unchecked exceptions are occurring during runtime only, not at compile time.
          

Example:



package com.exceptions;
public class ArithmeticExceptionTest {
       public static void main(String[] args) {
              int i=10/0;
              System.out.println(i);
       }
}


OutPut:


Exception in thread "main" java.lang.ArithmeticException: / by zero
       at com.exceptions.ArithmeticExceptionTest.main(ArithmeticExceptionTest.java:4)


Error:

Errors occur by the system resources and it is not recoverable by the programmer.

e.g: StackOverFlowError, OutOfMemoryError, NoClassDefFoundError and etc.

Example: OutOfMemoryError, it will occur whenever system doesn’t contain sufficient memory to run our program.

Example:


public class StackOverflow {
      
       void method1(){           
              method2(); //call method2
       }
      
       void method2(){
              method1(); //call method1
       }
      
       public static void main(String[] args) {
              StackOverflow obj=new StackOverflow();         
                     obj.method1();            
       }
}


Output

Exception in thread "main" java.lang.StackOverflowError
       at practise.StackOverflow.method1(StackOverflow.java:6)
       at practise.StackOverflow.method2(StackOverflow.java:10)
       at practise.StackOverflow.method1(StackOverflow.java:6)
       at practise.StackOverflow.method2(StackOverflow.java:10)
       at practise.StackOverflow.method1(StackOverflow.java:6)
       at practise.StackOverflow.method2(StackOverflow.java:10)
       at practise.StackOverflow.method1(StackOverflow.java:6)
       at practise.StackOverflow.method2(StackOverflow.java:10)



No comments:

Post a Comment