Java handles errors with try/catch/finally, and uniquely distinguishes checked exceptions (must be declared or handled) from unchecked exceptions (runtime, not enforced). Understanding this distinction is central to Java error handling.
Java handles errors with try/catch/finally, and uniquely distinguishes checked exceptions (must be declared or handled) from unchecked exceptions (runtime, not enforced). Understanding this distinction is central to Java error handling.
try {
FileReader f = new FileReader("file.txt"); // may throw IOException
// ... use it ...
} catch (FileNotFoundException e) {
System.out.println("not found: " + e.getMessage()); // specific first
} catch (IOException e) {
System.out.println("io error"); // broader after
} finally {
System.out.println("always runs — cleanup"); // runs no matter what
}
finally always executes (cleanup), even if an exception is thrown or a return occurs.
Throwable
├── Error — serious JVM problems (OutOfMemoryError) — don't catch
└── Exception
├── RuntimeException → UNCHECKED (not enforced by the compiler)
│ NullPointerException, IllegalArgumentException, IndexOutOfBounds...
└── (other Exceptions) → CHECKED (compiler enforces handling)
IOException, SQLException...
// MUST either catch it or declare `throws` — the compiler won't let you ignore it
public void readFile() throws IOException { // declare it
Files.readAllLines(Paths.get("file.txt")); // throws checked IOException
}
Checked exceptions (IOException, SQLException) represent recoverable, expected conditions (a file missing, a network failure). The compiler forces you to handle or declare them — making external-failure handling explicit.
// NOT required to declare or catch — usually indicate PROGRAMMING BUGS
String s = null;
s.length(); // NullPointerException (unchecked)
int x = arr[10]; // ArrayIndexOutOfBoundsException (unchecked)
Integer.parseInt("abc"); // NumberFormatException (unchecked)
Unchecked (RuntimeException subclasses) usually signal programming errors — you fix the bug rather than catch it everywhere. The compiler doesn't force handling.
// resources implementing AutoCloseable are closed automatically
try (FileReader f = new FileReader("file.txt")) {
// use f
} // f.close() called automatically, even on exception — no finally needed
✓ Catch specific exceptions, not bare Exception
✓ Use try-with-resources for files/connections (auto-close)
✓ Don't swallow exceptions silently (empty catch blocks)
✓ Throw meaningful exceptions; include context in messages
✓ Custom exceptions extend Exception (checked) or RuntimeException (unchecked)
Robust error handling is essential, and Java's checked/unchecked distinction is a defining (and debated) feature worth understanding deeply.
Checked exceptions force explicit handling of expected external failures (I/O, DB) — promoting reliability but sometimes criticized as verbose; unchecked exceptions represent bugs to fix rather than routinely catch.
Knowing the hierarchy, when each kind applies, the try/catch/finally semantics, and modern try-with-resources for automatic cleanup is fundamental to writing correct, reliable Java — and to designing your own exceptions appropriately.
The distinction shapes how Java APIs are designed and is a frequent interview and code-review topic.