Checked Exception in Java vs. Unchecked Exception in Java — What's the Difference?
By Tayyaba Rehman & Fiza Rafique — Published on January 30, 2024
Checked Exceptions in Java are exceptions that are checked at compile-time. Unchecked Exceptions in Java are exceptions that are not checked at compile-time.
Difference Between Checked Exception in Java and Unchecked Exception in Java
Table of Contents
ADVERTISEMENT
Key Differences
Checked exceptions in Java are exceptions that are checked at compile-time. They must be either caught in a try-catch block or declared in the method's throws clause. Unchecked exceptions in Java are exceptions that are not checked at compile-time. They occur mainly due to programming errors and include runtime exceptions and errors.
Checked exceptions in Java are exceptions indicate conditions that a reasonable application might want to catch, like IOException. Unchecked exceptions are usually the result of incorrect programming and should be fixed in the code, like NullPointerException.
Checked exceptions are subclasses of Exception, excluding RuntimeException and its subclasses. They represent invalid conditions in areas outside the immediate control of the program, like file not found errors. Unchecked exceptions include RuntimeException and its subclasses, and they are usually the programmer's responsibility to avoid. Unchecked exceptions often represent defects in the program (bugs), like logic errors or improper use of an API.
Comparison Chart
Compilation Check
Checked at compile-time
Not checked at compile-time
Handling Requirement
Must be handled or declared
Handling is optional
ADVERTISEMENT
Subclass
Subclass of Exception (excluding RuntimeException)
Subclass of RuntimeException
Example
IOException, SQLException
NullPointerException, ArithmeticException
Nature
External to the program, like file errors
Internal to the program, like logic errors
Compare with Definitions
Checked Exception in Java
Exceptions that need to be declared or handled in the code.
FileNotFoundException is a checked exception that must be handled in Java.
Unchecked Exception in Java
Runtime exceptions that Java does not check during compilation.
NullPointerException is an unchecked exception caused by accessing a null object reference.
Checked Exception in Java
Exceptions representing problems outside the program's control.
Reading a file may throw IOException, a checked exception in Java.
Unchecked Exception in Java
Exceptions not required to be caught or declared in Java.
ArithmeticException is an unchecked exception thrown for erroneous arithmetic operations.
Checked Exception in Java
Exceptions in Java that must be acknowledged in the method signature.
ClassNotFoundException is a checked exception for when Java can’t find a class.
Unchecked Exception in Java
Subclass of RuntimeException, indicating programmer mistakes.
IllegalArgumentException is an unchecked exception for passing invalid arguments.
Checked Exception in Java
Java exceptions checked at compile-time for proper handling.
SQLException is a checked exception, indicating potential database access issues.
Unchecked Exception in Java
Java exceptions representing bugs or logical errors in the program.
ClassCastException is an unchecked exception when trying to cast objects improperly.
Checked Exception in Java
A type of exception that forces error handling or declaration.
InterruptedIOException is a checked exception that occurs during I/O operations.
Unchecked Exception in Java
Exceptions that usually result from programming errors.
ArrayIndexOutOfBoundsException is an unchecked exception when accessing an invalid array index.
Common Curiosities
What is an unchecked exception in Java?
An unchecked exception is a runtime exception that is not checked at compile-time by Java.
Why are some exceptions checked in Java?
Checked exceptions are used for errors that a program should reasonably anticipate and recover from.
How do you handle a checked exception?
A checked exception is handled using a try-catch block or by declaring it with a throws clause.
What is a checked exception in Java?
A checked exception is an exception that Java requires to be caught or declared in the method.
What is the superclass of unchecked exceptions?
The superclass of unchecked exceptions is java.lang.RuntimeException.
What is the superclass of checked exceptions?
The superclass of most checked exceptions is java.lang.Exception.
What happens if you don't handle an Unchecked Exception in Java?
If you don't handle an Unchecked Exception, it will result in a runtime exception, potentially causing program termination.
Is it mandatory to catch or declare Unchecked Exceptions in Java?
No, it is not mandatory to catch or declare Unchecked Exceptions; you can choose to handle them or let them propagate up the call stack.
Are NullPointerExceptions checked or unchecked?
NullPointerExceptions are unchecked exceptions in Java.
Can unchecked exceptions be caught in Java?
Yes, unchecked exceptions can be caught, but it's not mandatory to do so.
Can Unchecked Exceptions be subclassed from RuntimeException?
Yes, Unchecked Exceptions are subclasses of RuntimeException.
In which situations do Unchecked Exceptions typically occur?
Unchecked Exceptions often occur due to programming errors, such as null references, invalid array indices, or incorrect method arguments.
When should you catch Unchecked Exceptions?
You should catch Unchecked Exceptions when you can recover from them or when you want to handle them gracefully.
Share Your Discovery
Previous Comparison
Atavism vs. Retrogressive EvolutionNext Comparison
Table Salt vs. Kosher SaltAuthor Spotlight
Written by
Tayyaba RehmanTayyaba Rehman is a distinguished writer, currently serving as a primary contributor to askdifference.com. As a researcher in semantics and etymology, Tayyaba's passion for the complexity of languages and their distinctions has found a perfect home on the platform. Tayyaba delves into the intricacies of language, distinguishing between commonly confused words and phrases, thereby providing clarity for readers worldwide.
Co-written by
Fiza RafiqueFiza Rafique is a skilled content writer at AskDifference.com, where she meticulously refines and enhances written pieces. Drawing from her vast editorial expertise, Fiza ensures clarity, accuracy, and precision in every article. Passionate about language, she continually seeks to elevate the quality of content for readers worldwide.