Sub topic 3
Checked vs Unchecked Exceptions
In the last chapter you saw the exception family tree. Java sorts every exception in that tree into one of two groups: checked or unchecked. Which group an exception is in decides whether the compiler makes you deal with it.
What the Two Words Mean
A checked exception is an exception the compiler checks for. If your code calls a method that can throw one, the compiler stops with an error until you handle it. Checked exceptions describe problems outside your program's control, like a missing file or a lost network connection.
An unchecked exception is an exception the compiler does not check for. Your code compiles even if you ignore it. Unchecked exceptions usually point to a bug in the code, like using a null value or reading past the end of an array.
The rule follows the family tree:
RuntimeExceptionand its subclasses are unchecked.Errorand its subclasses are also unchecked.- Every other subclass of
Exceptionis checked.
Common Examples
| Exception | Group | Typical cause |
|---|---|---|
| `IOException` | Checked | A file or stream cannot be read or written |
| `FileNotFoundException` | Checked | A file does not exist |
| `NullPointerException` | Unchecked | Calling a method on `null` |
| `ArrayIndexOutOfBoundsException` | Unchecked | Using an index outside the array |
| `NumberFormatException` | Unchecked | Parsing text that is not a number |
| `OutOfMemoryError` | Unchecked (Error) | The JVM runs out of memory |
An Unchecked Exception: Compiles, Then Crashes
This program compiles without complaint. It fails only when it runs.
public class UncheckedDemo {
public static void main(String[] args) {
String text = null;
System.out.println(text.length());
}
}When you run it, Java throws a NullPointerException and the program stops. The compiler never warned you because the exception is unchecked.
A Checked Exception: The Compiler Insists
Files.readString can throw IOException, which is checked. This version compiles and runs (Java 11 or later) because main declares the exception with throws IOException:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class CheckedDemo {
public static void main(String[] args) throws IOException {
String content = Files.readString(Path.of("notes.txt"));
System.out.println(content);
}
}Delete throws IOException and try to compile again. The compiler rejects the code with this message:
error: unreported exception IOException; must be caught or declared to be thrownThat message is called the catch-or-declare rule. For a checked exception you must do one of two things:
- Catch it with
tryandcatch, covered in the next chapter. - Declare it with a
throwsclause, covered in "Throwing and Declaring Exceptions".
Why Have Two Groups?
Checked exceptions cover problems that can happen even in perfect code. A file can be deleted at any time, so Java makes you plan for it. Unchecked exceptions mostly come from mistakes. If the compiler forced you to handle every possible null, almost every line would need handling code. You fix those bugs instead.
Exercise
Will this method compile? void load() { java.nio.file.Files.readString(java.nio.file.Path.of("a.txt")); } Explain why or why not.
Show answer
No. Files.readString can throw IOException, which is a checked exception. The method neither catches it nor declares throws IOException, so the compiler reports an "unreported exception" error.
Exercise
NumberFormatException extends IllegalArgumentException, which extends RuntimeException. Is NumberFormatException checked or unchecked?
Show answer
Unchecked. Any subclass of RuntimeException is unchecked, so the compiler does not force you to handle it.