Difference Between Checked And Unchecked Exception in Java

In this post we’ll see some of the differences between checked and unchecked exception in Java. Before going into checked exception Vs unchecked exception let’s try to understand which exception classifies as a checked exception and which one as unchecked.

Checked exception in Java

While writing Java code, for some of the code lines compiler will complain about the unhandled exception.

As Example – If you create an InputStream object for reading a file.

InputStream is = new FileInputStream(new File(“D:\\test.txt”));

This statement will result in compile time error “Unhandled exception type FileNotFoundException”.

This compile time error crops up because of the fact that the above statement may throw the type of exception which is classified as checked exception.

Unchecked exception in Java

You may have some business logic in your code which may result in an error at runtime some of the frequent causes are passing null or going beyond the array length.

As example –

The above code will result in ArrayIndexOutOfBoundsException at run time but you won’t get any error at compile time like we saw for checked exception. These types of exceptions for which compiler won’t enforce you to handle them are classified as unchecked exception.

Checked and unchecked exceptions in Java exception class hierarchy

In Java exception class hierarchy Throwable is at the top, parent class of all the exception classes. There are two direct descendants of Throwable class called Exception class and Error class.

Exception class has one subclass called RunTimeException.

checked unchecked exception Java

Exception Hierarchy

If an Exception class inherits from Exception (directly or indirectly) but not from RunTimeException class then it is a checked exception.

Any code that may throw a checked exception must be enclosed in a try-catch block to handle that exception or declare it using throws clause in method signature. Not doing so will result in compile time error.

All the exception classes inheriting from RunTimeException class plus the classes inheriting from Error class are collectively known as unchecked exceptions.

For unchecked exceptions there is no need to enclose the code in try-catch block or declare them using throws clause as it is not enforced by the compiler. Note that you can catch unchecked exceptions too or specify using throws clause it’s not enforced like in case of checked exception that’s all.

Checked exception classes

Some of the classes that are classified as checked exception in Java are listed below.

  • ClassNotFoundException
  • CloneNotSupportedException
  • FileNotFoundException
  • InstantiationException
  • IOException
  • ParseException

Unchecked exception classes

Some of the classes that are classified as unchecked exception in Java are listed below.

  • ArithmeticException
  • ArrayIndexOutOfBoundsException
  • ClassCastException
  • IllegalArgumentException
  • NullPointerException
  • NumberFormatException
  • ExceptionInInitializerError
  • OutOfMemoryError
  • StackOverflowError

Checked Vs Unchecked exception in Java

Now let’s try to list some differences between checked and unchecked exceptions in Java.

  1. Checked exceptions are to be caught using try-catch block or specified using throws clause. Not doing that will result in compile time error.
    Not handling or specifying unchecked exceptions won’t result in a compile time error.
  2. Checked exceptions are child classes of the Exception class where as unchecked exceptions are child classes of the RunTimeException class and child classes of Error class.
  3. Checked exceptions are those exceptions from which you are anticipated to recover from. That’s why checked exceptions are enforced by compiler because that reduces the number of exceptions which are not properly handled.
    On the other hand unchecked exceptions are mostly programming errors from which you can’t recover so it is not enforced to handle unchecked exception.

That’s all for the topic Difference Between Checked And Unchecked Exception in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

2 Comments

  1. Pingback: Exception Handling Best Practices in Java - KnpCode

  2. Pingback: Java Program to Count Number of Words in a String - KnpCode

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.