Difference Between Checked and Unchecked Exception in Java (with Comparison Chart)

Table of Contents

Checked-and-Unchecked-Exception3“Throwable” is the parent class of the classes Error and Exception. The class “RuntimeException” and its subclasses, the class “Error” and its child classes are the “Unchecked exceptions” whereas, the remaining subclasses of the class “Exception” except “RuntimeException” are the checked exceptions.

The basic difference between checked and unchecked exception is that the checked exceptions are checked by the compiler whereas, the compiler does not check the unchecked exceptions.

Let us discuss the other differences between checked and unchecked exceptions with the help of the comparison chart.

Content: Checked Vs Unchecked Exception

  • Comparison Chart
  • Definition
  • Key Differences
  • Conclusion
  • Comparison Chart

    Basis for ComparisonChecked ExceptionUnchecked Exception
    BasicThe compiler checks the checked exception.The compiler does not check the Unchecked exception.
    Class of ExceptionExcept "RuntimeException" class all the child classes of the class "Exception", and the "Error" class and its child classes are Checked Exception."RuntimeException" class and its child classes, are"Unchecked Exceptions".
    HandlingIf we do not handle the checked exception, then the compiler objects.Even if we do not handle the unchecked exception, the compiler doesn't object.
    CompilationThe program doesn't compile if there is an unhandled checked exception in the program code.The program compiles successfully even if there is an unhandled unchecked exception in the program code.

    Definition of Checked Exception

    The checked exceptions are those exceptions which are checked by the compiler for the smooth execution of the program. The compiler checks whether the program can handle the checked exception or not. If the code is unable to handle the exception there occurs a compilation error. All the subclasses of class “Exception” except the RuntimeException class are the checked exceptions.

    Let us study the checked exception with an example.

    import java.io.*; class Example { public static void main(String args[]) { FileInputStream fis = null; fis = new FileInputStream("B:/myfile.txt"); //This constructor FileInputStream(File filename) throws FileNotFoundException which is a checked exception. int k; while(( k = fis.read() ) != -1) { //Method read() of FileInputStream class also throws a checked exception: IOException. System.out.print((char)k); } fis.close(); //The method close() closes the file input stream it throws IOException. } }

    In above code, we are trying to open, read and display file contents. It may happen that the file is not present, then FileNotFoundException will occur which is a checked exception.

    If checked exception is not handled, then the compiler won’t allow the program to be compiled smoothly. It will show compile time error. So, for smooth compilation, the checked exception must be caught or declared to be thrown.

    Definition of Unchecked Exception

    Unchecked Exceptions are those exceptions which are not checked by the compiler. The compiler compiles the program successfully even if the exceptions are not handled by the code. The class “Error” and its child classes, the class “Runtime” and its subclasses are the unchecked exceptions.

    Let us see an example of an unchecked exception.

    class Example { public static void main(String args[]) { int arr[] ={1,2,3,4,5}; System.out.println(arr[7]); //ArrayIndexOutOfBoundsException. } }

    In above code, you can see that there is an ArrayIndexOutOfBoundsException, as I am trying to access the element which does not exist. As this is an unchecked exception, compile time error will not occur, and the file will be compiled without any error. But the program won’t execute till the exception is handled. So, for smooth execution, the exceptions must be caught or declared to be thrown.

    Key Differences Between Checked and Unchecked Exception

  • Checked exceptions are in knowledge of compiler whereas, Unchecked exceptions are not in knowledge of compiler.
  • Except RuntimeException and Error class all the classes are checked exception. On the other hand, RuntimeException and Error classes are unchecked exceptions.
  • If the checked exceptions are not handled by the code then the compiler objects. On the other hand, if we do not handle unchecked exceptions in the code the compiler doesn’t object.
  • If checked exceptions occur in the code the code will not compile whereas, even if unchecked exceptions are not handled by the code the compiler still compiles the code.
  • Note

    Both checked or unchecked exception compulsorily occurs during runtime. They are only checked or unchecked by the compiler during compile time.

    Conclusion

    Both the checked and unchecked exceptions must be handled in order to execute the program smoothly.

    ncG1vNJzZmislZi1pbXFn5yrnZ6YsrR6wqikaJyZm7OmvsSnmp5lkprBuLHEp2ScoJWYuKawjJqlnWWlo7CpscKknJ1lla2wprzToqanZWJjtbW5yw%3D%3D