Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

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

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
Chaturmind
← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
HomeLearnJava Interview PrepJava Interview Prep: Fresher to 2 YearsStrings, Keywords, Exceptions & Serialization
✓ FreeBeginner· 8 min read

Exceptions, Generics, Enums & Reflection — Interview Questions

The exception hierarchy, checked vs unchecked, try/catch/finally edge cases (return, System.exit, multi-catch), finally vs finalize, generics and type erasure, enums, and reflection.

Published September 25, 2026


How to use this lesson

Exception questions tend to escalate quickly: "what's a checked exception?" becomes "what happens if both try and finally return?". Know the hierarchy, the edge cases, and the modern idioms (try-with-resources, multi-catch).

Q1. What are exceptions in Java?

Short answer: An exception is an object representing an abnormal event that interrupts normal flow: a missing file, invalid input, a null reference. When one is thrown, the JVM unwinds the call stack until it finds a matching catch. If nothing catches it, the thread terminates and prints the stack trace.

Key points to cover:

  • The hierarchy: Throwable → Error (JVM problems) and Exception → RuntimeException (unchecked) plus the other checked exceptions.
  • Exceptions carry a message, a cause (for chaining), and a stack trace.
Throwable
├── Error                 (OutOfMemoryError, StackOverflowError) — don't catch
└── Exception             (checked: IOException, SQLException)
    └── RuntimeException  (unchecked: NullPointerException, IllegalArgumentException)

Learn it in depth → Exception Handling

Q2. What's the difference between checked and unchecked exceptions?

Short answer: Checked exceptions (subclasses of Exception, but not of RuntimeException) are verified at compile time. You must either catch them or declare them with throws. Unchecked exceptions (RuntimeException, Error and their subclasses) don't need to be declared or caught.

CheckedUnchecked
ExamplesIOException, SQLException, InterruptedExceptionNullPointerException, IllegalArgumentException, ArithmeticException
Compiler enforces handlingYesNo
Typically meansA recoverable external conditionA programming bug, or a violated precondition

Key points to cover:

  • Both kinds happen at runtime. "Checked" refers only to the compiler checking that they're handled.
  • Modern frameworks (Spring, JPA) mostly use unchecked exceptions. Spring translates SQLException into its unchecked DataAccessException hierarchy.

Learn it in depth → Exception Handling

Q3. What are the roles of try, catch and finally?

Short answer:

  • try wraps the code that might throw.
  • catch handles a specific exception type.
  • finally always runs afterwards, whether or not an exception occurred, and is used for cleanup.
Connection con = null;
try {
    con = dataSource.getConnection();
    // work
} catch (SQLException e) {
    throw new OrderPersistenceException("could not save order", e);   // wrap, keeping the cause
} finally {
    if (con != null) try { con.close(); } catch (SQLException ignored) { }
}

// Modern equivalent: try-with-resources closes automatically, in reverse order
try (Connection c = dataSource.getConnection();
     PreparedStatement ps = c.prepareStatement(SQL)) {
    ps.executeUpdate();
}

Key points to cover:

  • Catch blocks are checked in order, so put the more specific types first. A broader type before a narrower one is a compile error ("exception has already been caught").
  • Prefer try-with-resources (any AutoCloseable) to manual finally cleanup. It also preserves "suppressed" exceptions thrown by close().

Q4. If try or catch executes a return, does finally still run?

Short answer: Yes. finally runs after the return value is computed but before control goes back to the caller.

static int demo() {
    int x = 1;
    try { return x; }          // the return value 1 is saved here
    finally { x = 99; }        // runs, but doesn't change the saved value
}                              // demo() returns 1

static int bad() {
    try { throw new RuntimeException("lost"); }
    finally { return 2; }      // a return in finally SWALLOWS the exception, and returns 2
}

Common trap: a return (or throw) inside finally silently discards any exception or return value from try. Never return from finally.

Q5. Can you use try without catch?

Short answer: Yes. try–finally (no catch) runs cleanup while letting the exception propagate to the caller. try-with-resources can also stand alone, with no catch and no finally.

lock.lock();
try {
    updateSharedState();
} finally {
    lock.unlock();            // always released, even if updateSharedState() throws
}

Q6. Does exception handling affect performance?

Short answer: A try block itself costs essentially nothing. Throwing an exception is what's expensive, mainly because the stack trace is captured, and unwinding the stack takes time. So exceptions are fine for exceptional situations, but they shouldn't be used for normal control flow.

Key points to cover:

  • Don't use exceptions to exit loops, or as return values in hot paths. Validate first (if (map.containsKey(k))), or return Optional.
  • For high-frequency, expected failures, some libraries create exceptions without stack traces, using the Throwable(msg, cause, false, false) constructor.

Q7. When does a finally block not execute?

Short answer:

  • When the JVM stops before reaching it: System.exit() in try or catch, Runtime.halt(), a JVM crash, or the process being killed (kill -9, power loss).
  • When the try block never finishes, because of an infinite loop or a deadlock.
  • When the thread is a daemon and the JVM exits first.

Key points to cover:

  • System.exit() runs shutdown hooks, but not pending finally blocks.

Q8. Can a try have multiple finally blocks? How do you handle several exceptions in one catch?

Short answer: No. Each try can have at most one finally (but many catch blocks). To handle several exception types the same way, use multi-catch (Java 7+):

try {
    importFile(path);
} catch (IOException | ParseException e) {       // e is effectively final here
    log.warn("import failed for {}", path, e);
}

Key points to cover:

  • The types in a multi-catch can't be subclasses of one another (IOException | Exception is a compile error, because it's redundant).

Q9. What's the difference between Throwable, Exception and Error?

Short answer: Throwable is the root of everything that can be thrown. Exception represents conditions an application might reasonably handle. Error represents serious JVM-level problems (OutOfMemoryError, StackOverflowError, NoClassDefFoundError) that applications normally shouldn't catch.

Key points to cover:

  • Catching Throwable or Error broadly hides fatal problems. The exception is top-level logging in thread pools or frameworks.

Q10. What's the difference between finally and finalize()?

Short answer: finally is a language block that runs deterministically after try/catch. finalize() was a method the garbage collector might call before reclaiming an object. It's deprecated, unpredictable, and it may never run at all: for example, if the object is never collected, or the JVM exits first.

Learn it in depth → Garbage Collection Fundamentals

Q11. What are generics?

Short answer: Generics let classes, interfaces and methods take type parameters (List<String>, Map<K, V>, <T> T first(List<T>)). They give compile-time type safety, which means no casts and no runtime ClassCastException from putting the wrong type in a collection, and they let one implementation serve many types.

List raw = new ArrayList();            // pre-Java 5: anything goes
raw.add("x"); Integer n = (Integer) raw.get(0);    // ClassCastException at runtime

List<String> names = new ArrayList<>();
names.add("Asha");
// names.add(42);                      // compile error: caught early
String first = names.get(0);           // no cast needed

Key points to cover:

  • Type erasure: generic type information is removed at compile time (List<String> becomes List in bytecode). That's why you can't do new T(), can't use instanceof List<String>, and can't create a new T[] array.
  • Bounded types: <T extends Comparable<T>>. Wildcards: ? extends T for reading (producer), and ? super T for writing (consumer), known as PECS.

Learn it in depth → Generics

Q12. What is an enum in Java?

Short answer: An enum is a type with a fixed set of named constants, such as OrderStatus { PLACED, PAID, SHIPPED, DELIVERED }. Java enums are full classes: they can have fields, constructors, methods, and even per-constant behaviour.

public enum OrderStatus {
    PLACED(false), PAID(false), SHIPPED(false), DELIVERED(true), CANCELLED(true);

    private final boolean terminal;
    OrderStatus(boolean terminal) { this.terminal = terminal; }
    public boolean isTerminal() { return terminal; }
}

Key points to cover:

  • Enums are type safe (you can't pass a random int or String), and they work in switch (with exhaustiveness checks in switch expressions).
  • They're implicitly final, and each constant is a singleton. EnumMap and EnumSet are very fast collections keyed by enums.
  • Store the name (@Enumerated(EnumType.STRING)) in databases, never the ordinal. Reordering the constants would silently corrupt the data.

Q13. What is reflection?

Short answer: Reflection (java.lang.reflect) lets code inspect and use classes at runtime: list fields, methods, constructors and annotations, create instances, call methods, and read or write fields, even private ones, when access is allowed.

Class<?> type = Class.forName("com.shop.Order");
Object order = type.getDeclaredConstructor().newInstance();
Method m = type.getMethod("setStatus", String.class);
m.invoke(order, "PAID");

Key points to cover:

  • Frameworks are built on it: Spring (dependency injection, reading annotations), Hibernate (mapping fields), Jackson (JSON), JUnit (finding @Test methods).
  • Downsides: it's slower, gets no compile-time checks, and breaks encapsulation. The module system (Java 9+) restricts deep reflection into packages that aren't opened.

Follow-up questions this topic invites — and their answers

Q: What is exception chaining? A: Wrapping a low-level exception as the cause of a higher-level one (new ServiceException("…", e)), so the root cause and its stack trace aren't lost. Always pass the cause when rethrowing.

Q: How do you create a custom exception? A: Extend RuntimeException (or Exception if callers must handle it), and provide constructors that take a message and a cause: class InsufficientStockException extends RuntimeException { … }.

Q: Is throw different from throws? A: throw actually throws an exception object inside a method body. throws in a method signature declares which checked exceptions the method may propagate.

Q: What is NoClassDefFoundError vs ClassNotFoundException? A: ClassNotFoundException is a checked exception from explicit dynamic loading (Class.forName) when the class isn't found. NoClassDefFoundError is an Error raised when a class that was present at compile time is missing at runtime, or failed its static initialisation.

Previous

static & final Keywords — Interview Questions

Next

Serialization & transient — Interview Questions

AI Tutor

Lesson: Exceptions, Generics, Enums & Reflection — Interview Questions

Quick actions

AI responses can be inaccurate. Verify critical information.