100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Java Exception Handling Cheat Sheet

Java Exception Handling Cheat Sheet

Covers try/catch/finally blocks, try-with-resources, custom checked exceptions, checked versus unchecked exception types, and exception cause chaining in Java.

2 PagesIntermediateMar 30, 2026

try/catch/finally

Core exception-handling syntax, including multi-catch ordering.

java
try {    int result = 10 / 0; // throws ArithmeticException} catch (ArithmeticException e) {    System.err.println("Math error: " + e.getMessage());} catch (Exception e) { // broader catch, must come after specific ones    System.err.println("Unexpected: " + e.getMessage());} finally {    System.out.println("Always runs, even after return"); // cleanup}

try-with-resources (Java 7+)

Automatically close AutoCloseable resources, even on exception.

java
// Resource must implement AutoCloseable; close() is called automaticallytry (BufferedReader reader = new BufferedReader(new FileReader("data.txt"));     FileWriter writer = new FileWriter("out.txt")) {    String line = reader.readLine();    writer.write(line);} catch (IOException e) {    System.err.println("I/O failed: " + e.getMessage());} // both resources closed in reverse order, even on exception

Custom Exceptions

Define a checked exception that carries extra diagnostic data.

java
public class InsufficientFundsException extends Exception { // checked exception    private final double shortfall;    public InsufficientFundsException(String message, double shortfall) {        super(message);        this.shortfall = shortfall;    }    public double getShortfall() { return shortfall; }}public void withdraw(double amount) throws InsufficientFundsException {    if (amount > balance) {        throw new InsufficientFundsException("Not enough funds", amount - balance);    }}

Checked vs. Unchecked Exceptions

The distinction that determines whether the compiler forces handling.

  • Checked exceptions- Extend Exception (not RuntimeException); must be declared with throws or caught, e.g. IOException
  • Unchecked exceptions- Extend RuntimeException; not required to be declared or caught, e.g. NullPointerException
  • Error- Represents serious JVM-level problems (OutOfMemoryError); not meant to be caught
  • throws clause- Declares that a method may propagate a checked exception to its caller
  • throw- Statement that actually raises an exception instance
  • Multi-catch- catch (IOException | SQLException e) handles multiple types in one block (Java 7+)
  • Exception chaining- new CustomException("msg", cause) preserves the original exception via getCause()

Exception Chaining

Wrap a lower-level exception while preserving its original cause.

java
try {    parseConfig();} catch (ParseException e) {    throw new RuntimeException("Failed to start application", e); // wraps original cause}// Later:catch (RuntimeException e) {    System.err.println(e.getMessage());    System.err.println("Root cause: " + e.getCause());}
Pro Tip

Never catch an exception and swallow it silently (empty catch block) - at minimum log it with the stack trace (e.printStackTrace() or a logging framework), since a silent catch turns a debuggable failure into a mysterious one.

Was this cheat sheet helpful?

Explore Topics

#JavaExceptionHandling#JavaExceptionHandlingCheatSheet#Programming#Intermediate#TryCatchFinally#TryWithResourcesJava7#CustomExceptions#CheckedVsUncheckedExceptions#ErrorHandling#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet