Checked and unchecked exceptions
What you will learn
Exceptions report failures that prevent a computation from continuing normally. Java uses exception objects to transfer control to a handler.
- Distinguish checked exceptions from unchecked exceptions and errors.
- Choose whether to handle or propagate a checked exception.
Each example is a standalone Main.java. Compile with javac --release 21 Main.java and run with java Main. Java 25 execution remains unverified.
public class Main { static void verify(boolean ready) throws java.io.IOException { if (!ready) throw new java.io.IOException("Catalog unavailable"); } public static void main(String[] args) { try { verify(false); } catch (java.io.IOException e) { System.out.println(e.getMessage()); } System.out.println("Ready for next request"); }}Expected output:
Catalog unavailableReady for next requestThe method declares a possible checked IOException; the caller handles it and can proceed.
The exception hierarchy and compile-time checking
Throwable is the root type of objects Java can throw. Exception and Error are its direct subclasses. RuntimeException and its subclasses, together with Error and its subclasses, are unchecked; the remaining Throwable types are checked. Ordinary applications generally should not catch Error to recover from serious JVM or linkage failures. A checked exception that can escape a method must be covered by its throws declaration unless handled within the method. Unchecked exceptions can also be declared, but are not required to be. A throws clause describes possible failure; it does not itself throw anything. A throw statement actually transfers control. A method can return normally even with a throws clause. Choose the recovery boundary based on whether the caller can respond meaningfully, not simply on whether the compiler complains. IOException here is deliberately constructed; no file access is necessary.
public class Main { static int quantity(int n) { if (n < 0) throw new IllegalArgumentException("Negative quantity"); return n; } public static void main(String[] args) { try { quantity(-2); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } System.out.println(quantity(3)); }}Expected output:
Negative quantity3The unchecked exception needs no throws declaration. The valid call still returns its value.
A practical variation
import java.io.IOException;public class Main { static void read() throws IOException { throw new IOException("Unavailable"); } static void load() throws IOException { read(); } public static void main(String[] args) { try { load(); } catch (IOException e) { System.out.println("Load failed: " + e.getMessage()); } }}Expected output:
Load failed: UnavailableBoth methods allow the checked failure to propagate; main chooses a useful response.
Common mistakes
Do not catch Throwable or ignore an IOException to silence the compiler. If you cannot recover locally, propagate it with a suitable contract.
Practice
Write a checked exception for an unavailable shelf and a method requireShelf(boolean available). On false report Shelf unavailable; on true report Shelf ready. The caller must handle the checked exception.
Hints:
- Extend Exception; add serialVersionUID for lint-clean source.
- Use throws on the method and catch the specific exception at the caller.
Related reference topics: Java Checked exceptions, Java throws clause, Java IllegalArgumentException. Next: Try catch finally and throwing.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Checked and unchecked exceptions — knowledge check
Apply Java exception and inventory contracts using Java 21-compatible examples.
Which type is checked?
What does throws IOException do?
Which action is valid for an escaping checked exception?
Is Error generally an ordinary recovery target?
What happens after a matching catch finishes normally?