Enclose exception-throwing code in a try block followed directly by one or more catch blocks; no code can appear between the try block's end and the first catch. Short methods can use a single try block with multiple handlers. Order catch blocks from specific to general: IOException is a super type of FileNotFoundException, so catching IOException first leaves the FileNotFoundException handler unreachable and the compiler reports an error. The compiler flags checked exceptions like the FileWriter constructor's IOException but not unchecked ones like IndexOutOfBoundsException from list.get(i). Put cleanup code in a finally block, which always executes when the try block exits and prevents resource leaks, or use the try-with-resources statement from Java SE 7 to close AutoCloseable resources automatically. Scope: Java. Reviewed October 4, 2026.
Catching and handling exceptions
Document v1 · published by Koskamo editorial · updated 2026-10-04
Source and provenance
- Original source
- dev.java
- Published on Koskamo by
- Koskamo editorial
- Citation version
- Document v1
- Reuse
- Original summaries; linked sources retain their own terms.