Aug 6, 2024•1 min read
Java Handling Exceptions Summary
Core exception-handling notes covering constructors, checked vs unchecked exceptions, and try-with-resources behavior.
title: "Java Handling Exceptions Summary" date: "2024-08-06" excerpt: "Core exception-handling notes covering constructors, checked vs unchecked exceptions, and try-with-resources behavior."
tags: "java,exceptions,error-handling" source: "https://shrikant-havale.in/2024/08/06/java-exception-handling/"
A concise summary of Java exception handling fundamentals.
Exception Constructors
Exception supports multiple constructor signatures including:
- no-arg
- message-only
- cause-only
- message + cause
- advanced constructor with suppression and writable stack trace flags
AutoCloseable
AutoCloseabledefinesclose().- Overridden
close()may throw narrower/no exceptions. - In try-with-resources, resources close in reverse declaration order.
- If both try block and close throw exceptions, close exceptions are suppressed.
Try-with-Resources
- Supports multiple resources.
- Variables in resource list must be final or effectively final.
Checked vs Unchecked
RuntimeExceptionand subclasses are unchecked.IOExceptionis checked.- Custom exceptions extending
Exceptionare checked unless extendingRuntimeException.
Catching Broad Types
- Catching
ThrowableorErroris generally discouraged in application code.
Source
Original post: https://shrikant-havale.in/2024/08/06/java-exception-handling/