Checked Exceptions vs Unchecked Exceptions in Java

Recently in an interview, I was asked to explain the difference between a checked exception and an unchecked exception in Java. This was one of the questions I couldn’t answer, so I did some research later tofind out the difference between a checked exception and an unchecked exception in Java.

The main difference between checked exceptions and unchecked exceptions regards compile-time. The Java compiler forces the programmer to catch the checked exception and handle it using a try-catch block. On the other hand, unchecked exceptions are runtime exceptions (they literally extend RuntimeException) and therefore do not need to be handle on compile time.

Examples of unchecked exceptions (extend java.lang.RuntimeException):

  • ArithmaticException
  • NullPointerException
  • IndexOutOfBoundsException
  • NegativeArraySizeException
  • ArrayStoreException

Examples of checked exceptions (extend java.lang.Exception):

  • ClassNotFoundException
  • InterruptedException
  • CloneNotSupportedException
Checked Exceptions vs Unchecked Exceptions
The class hierarchy outlining the differences between checked exceptions and unchecked exceptions in Java.

Leave a Reply

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