Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Core Fundamentals

Object-Oriented Programming

  • Classes and Objects
  • Inheritance and Polymorphism
  • Interfaces and Abstract Classes

Collections Framework

  • List, Set, and Map
  • Generics

Exceptions & Best Practices

  • Exception Handling
  • equals() and hashCode()
  • String Manipulation
Chaturmind
← Java Core Fundamentals

Object-Oriented Programming

  • Classes and Objects
  • Inheritance and Polymorphism
  • Interfaces and Abstract Classes

Collections Framework

  • List, Set, and Map
  • Generics

Exceptions & Best Practices

  • Exception Handling
  • equals() and hashCode()
  • String Manipulation
HomeLearnJavaJava Core FundamentalsExceptions & Best Practices
✓ FreeBeginner· 10 min read

Exception Handling

Handle errors gracefully — checked vs unchecked, try-with-resources, custom exceptions.

Published January 22, 2025


Exception Handling

Checked vs Unchecked Exceptions

Checked exceptions (subclasses of Exception, not RuntimeException) must be caught or declared:

public String readFile(String path) throws IOException {
    return Files.readString(Path.of(path)); // checked — must declare or catch
}

Unchecked exceptions (subclasses of RuntimeException) don't need to be declared:

public int divide(int a, int b) {
    return a / b; // ArithmeticException if b == 0 — unchecked
}

Try-with-resources (Java 7+)

Automatically closes AutoCloseable resources:

try (InputStream in  = new FileInputStream("input.txt");
     OutputStream out = new FileOutputStream("output.txt")) {
    // use in and out
} // both closed automatically, even if an exception is thrown

Custom Exceptions

// Unchecked custom exception (recommended for most cases)
public class OrderNotFoundException extends RuntimeException {
    private final String orderId;

    public OrderNotFoundException(String orderId) {
        super("Order not found: " + orderId);
        this.orderId = orderId;
    }

    public String getOrderId() { return orderId; }
}

Best Practices

  • Catch specific exceptions, not Exception or Throwable
  • Don't swallow exceptions — at minimum, log them
  • Use try-with-resources for anything that holds a connection or file handle
  • Prefer unchecked for programming errors (null, invalid arguments); use checked for recoverable conditions (file not found)
// ❌ Don't do this
try { ... } catch (Exception e) { } // silent swallow

// ✅ Do this
try { ... } catch (IOException e) {
    log.error("Failed to read file: {}", path, e);
    throw new FileProcessingException("Failed to process " + path, e);
}

Interview Tip

Interviewers often ask: "When would you use a checked exception vs an unchecked exception?"

Use checked when the caller can reasonably recover (file not found → prompt user for a different path). Use unchecked for programmer errors (null pointer, illegal argument) where recovery is not expected.

Previous

Generics

Next

equals() and hashCode()

AI Tutor

Lesson: Exception Handling

Quick actions

AI responses can be inaccurate. Verify critical information.