Exceptions: interview questions & scenarios
These are the exception questions I hear most in Java backend interviews, from the "what does finally return?" puzzles in a first screen to the production scenarios senior rounds open with. Exceptions look like syntax, but every good question here is really about what the JVM does when something goes wrong, and what your service does next.
Here's how to use this page: read the question, say your answer out loud first, then open the model answer. The "What if..." follow-ups are the ones interviewers actually ask next. If a question stumps you, the lessons are in this topic: How exceptions work in Java, Checked vs unchecked exceptions, try-with-resources and Custom exceptions and best practices.
What's the difference between checked and unchecked exceptions?
Tests catch-or-declare, RuntimeException, Error
Show answerHide answer
Model answer
Checked exceptions are every Throwable except RuntimeException, Error and their subclasses. The compiler enforces "catch or declare": if a method can throw IOException, you either catch it or add throws IOException. Unchecked exceptions (RuntimeException and Error subtrees) have no such rule.
It's purely a compile-time distinction. The bytecode and the JVM treat them identically, which is why tricks like "sneaky throw" can throw a checked exception without declaring it. My default: checked when the immediate caller can realistically recover, unchecked for programming errors and for failures handled at a generic boundary.
How I’d say it in the interview
"Checked exceptions are the ones the compiler forces you to catch or declare: everything except RuntimeException, Error and their subclasses. It's only a compiler rule; the JVM doesn't know the difference. I use checked when the direct caller can recover, unchecked otherwise."
Signals depth
Saying it's compile-time only, and giving a rule for choosing rather than a list of examples.
Red flags
"Checked exceptions happen at compile time and unchecked at runtime." Both are thrown at runtime.
Follow-ups interviewers ask next
What if an overriding method declares a new checked exception?
It doesn't compile. An override may declare fewer or narrower checked exceptions, never new or broader ones, because callers using the parent type wouldn't know to handle them. It may declare any unchecked exceptions.
What if the checked exception happens inside a lambda passed to
stream().map()?Function.applydeclares nothing, so it won't compile. Catch it inside the lambda and wrap it (for example inUncheckedIOExceptionwith the cause), then unwrap at the boundary if needed.
Walk me through the exception hierarchy. Should you ever catch an Error?
Tests Throwable, Error vs Exception, RuntimeException
Show answerHide answer
Model answer
Everything you can throw extends Throwable, which has two branches. Error is for problems in the JVM or environment that an application shouldn't try to handle. Exception is for problems your code can handle, and its RuntimeException subtree is the unchecked part:
The Throwable hierarchy: Error with OutOfMemoryError, StackOverflowError and NoClassDefFoundError, unchecked and not to be caught; Exception splitting into RuntimeException with NullPointerException, IllegalArgumentException and IllegalStateException, unchecked; and everything else, such as IOException, SQLException and InterruptedException, checked
In business code, no, don't catch Error. After an OutOfMemoryError the JVM may not even be able to allocate for your recovery code, and swallowing it keeps a broken process alive. A top-level handler may log it, and frameworks sometimes catch StackOverflowError around user-supplied recursion, but that's the exception, not the rule.
How I’d say it in the interview
"Throwable splits into Error and Exception. Errors like OutOfMemoryError mean the JVM is in trouble, so I let them crash the process and restart. Exception is what I handle, and RuntimeException is its unchecked branch. Everything else under Exception is checked."
Signals depth
Knowing Error is also unchecked, and arguing that a clean crash and restart beats a zombie process.
Red flags
catch (Throwable t) in business code "to be safe".
Follow-ups interviewers ask next
What if a static initializer throws?
The first access gets
ExceptionInInitializerError, and every later access getsNoClassDefFoundError: Could not initialize class Xwithout the original cause. Look for the first error in the logs.
What does each method return?
Tests finally semantics, return value capture
Model answer, how to say it, 2 follow-ups: unlock with the Java Pass
How do you create a custom exception, and how do you decide between checked and unchecked?
Tests custom exception design, cause, fields
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What is try-with-resources, and what happens if both the body and close() throw?
Tests AutoCloseable, suppressed exceptions, close order
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Your batch job dies after a few hours with "Too many open files". Walk me through it.
Tests resource leaks, try-with-resources, file descriptors
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Does finally always run?
Tests finally guarantees, JVM exit
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What is exception chaining, and what's wrong with throw new ServiceException(e.getMessage())?
Tests cause, exception translation
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Will this compile?
Tests catch ordering, multi-catch rules, unreachable catch
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
The try block throws, then the finally block throws too. Which exception does the caller see?
Tests finally overriding exceptions, suppressed
Model answer, how to say it: unlock with the Java Pass
Every gateway timeout shows up three or four times in our logs, each with a full stack trace, and the alert count tripled. What's going on?
Tests log-and-rethrow, logging at the boundary
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
How would you map exceptions to HTTP status codes in a REST service?
Tests exception hierarchy, boundary mapping, retryability
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Production logs show java.lang.NullPointerException with no message and no stack trace. Why?
Tests OmitStackTraceInFastThrow, JIT, helpful NPE messages
Model answer, how to say it: unlock with the Java Pass
After a deploy, pods take the full 30 s grace period to stop and get killed. A thread dump shows a worker loop still running. The loop catches InterruptedException and logs it. What's wrong?
Tests interrupt status, InterruptedException handling, shutdown
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
A team throws CacheMissException on every cache miss. At peak, CPU went up 20%. Why are exceptions expensive, and what would you do?
Tests fillInStackTrace cost, exceptions as control flow, stackless exceptions
Model answer, how to say it: unlock with the Java Pass
Is it ever right to write catch (Throwable t)?
Tests Error handling, top-level handlers
Model answer, how to say it: unlock with the Java Pass
Premium question set
Keep reading with the Java Pass
The rest of this question set is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.
From ₹199 · one-time payment · No auto-renewal