Checked vs Unchecked Exception in Java: Key Differences and Usage
checked exceptions are exceptions that the compiler forces you to handle explicitly, either by catching or declaring them. Unchecked exceptions are runtime exceptions that the compiler does not require you to handle, often caused by programming errors.Quick Comparison
Here is a quick table summarizing the main differences between checked and unchecked exceptions in Java.
| Aspect | Checked Exception | Unchecked Exception |
|---|---|---|
| Definition | Exceptions checked at compile-time | Exceptions checked at runtime |
| Handling | Must be caught or declared | No requirement to catch or declare |
| Common Superclass | Exception (excluding RuntimeException) | RuntimeException and its subclasses |
| Cause | External factors (e.g., file not found) | Programming errors (e.g., null pointer) |
| Examples | IOException, SQLException | NullPointerException, ArithmeticException |
| Compiler Enforcement | Yes | No |
Key Differences
Checked exceptions are exceptions that the Java compiler forces you to handle explicitly. This means you must either catch them with a try-catch block or declare them in the method signature using throws. They usually represent conditions outside the program's control, like missing files or network errors.
On the other hand, unchecked exceptions are subclasses of RuntimeException. The compiler does not require you to handle or declare them. These exceptions often indicate programming mistakes, such as accessing a null object or dividing by zero.
Because checked exceptions require explicit handling, they help make your code more robust by forcing you to think about error conditions. Unchecked exceptions are used for errors that are usually bugs and should be fixed in code rather than caught.
Code Comparison
import java.io.*; public class CheckedExceptionExample { public static void main(String[] args) { try { readFile("nonexistent.txt"); } catch (IOException e) { System.out.println("Caught checked exception: " + e.getMessage()); } } static void readFile(String filename) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(filename)); System.out.println(reader.readLine()); reader.close(); } }
Unchecked Exception Equivalent
public class UncheckedExceptionExample { public static void main(String[] args) { String text = null; try { System.out.println(text.length()); } catch (NullPointerException e) { System.out.println("Caught unchecked exception: " + e); } } }
When to Use Which
Choose checked exceptions when you expect the caller to handle recoverable conditions, such as file I/O errors or database issues. They make error handling explicit and improve program reliability.
Choose unchecked exceptions for programming errors that should be fixed in code, like null references or illegal arguments. These exceptions indicate bugs and usually should not be caught but corrected.
In summary, use checked exceptions for external, recoverable problems and unchecked exceptions for internal, programming mistakes.