In this post, we will learn the difference between Checked Exceptions and Unchecked Exceptions in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.
Difference between Checked Exceptions and Unchecked Exceptions in Java
Features | Checked Exceptions | Unchecked Exceptions |
---|---|---|
Detection Time | They are known at compile time. | They are known at run time. |
Checking | They are checked at compile time. | They are not checked at compile time. They occur only at run time. |
Type of Exceptions | These are compile-time exceptions. | These are run-time exceptions. |
Error Handling | If these exceptions are not handled properly, they give a compile-time error. | If these exceptions are not handled properly, they don’t give a compile-time error. However, the application will be prematurely terminated at run time. |
Class Hierarchy | All subclasses of java.lang.Exception Class, except subclasses of RuntimeException, are checked exceptions. | All subclasses of RuntimeException and subclasses of java.lang.Error are unchecked exceptions. |