September 15, 2022

Checked Vs Unchecked Exception in Java

In this post we’ll see some of the differences between checked and unchecked exceptions 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. For example, if you create an InputStream object for reading a file in Java.

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 (FileNotFoundException) which is classified as checked exception in Java.

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. For example-

int[] numArr = {4,5,6};
int num = numArr[4];

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 in Java.

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

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 in Java

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

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

Unchecked exception classes in Java

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 Checked Vs 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

No comments:

Post a Comment