Exception Handling
Java's exception model: the Throwable hierarchy, the checked and unchecked split, try/catch/finally and try-with-resources, throwing and declaring, custom types, and the handling habits that separate good code from noise. Interviewers use it to probe both language rules and design judgment.
part ofJavaoverview, primer and where to startread it →on this pageshowhide
explore
- Exception Hierarchy10 questions
- Throwable Hierarchy5 questions
- Checked vs Unchecked Exceptions5 questions
- Handling Exceptions20 questions
- try-catch and Catch Ordering5 questions
- Multi-catch5 questions
- finally Semantics5 questions
- try-with-resources5 questions
- Throwing and Declaring19 questions
- throw Statement5 questions
- throws Clause and Declaration Requirement5 questions
- Exception Propagation5 questions
- Rethrowing and More-Precise Rethrow4 questions
- Custom Exceptions14 questions
- Defining Custom Exception Classes5 questions
- Exception Chaining5 questions
- Checked vs Unchecked Design Choice4 questions
- Best Practices19 questions
- Catch Specific, Don't Swallow4 questions
- Exception Translation5 questions
- Fail-Fast and Avoiding Control Flow5 questions
questions
82 · 5 sectionsWhat is the difference between a checked and an unchecked exception in Java?
basics
~10 sA checked exception must be either caught or declared with throws, and the compiler enforces this. An unchecked exception (RuntimeException or Error and their subclasses) is not enforced by the compiler.
Name several common RuntimeException types and explain what each one signals about a likely bug in the code.
basics
~10 sCommon ones: NullPointerException (used a null), IllegalArgumentException (bad argument passed in), IllegalStateException (called at the wrong time), ClassCastException (bad cast), ArrayIndexOutOfBoundsException (index past the array end), and NumberFormatException (parsed text that isn't a number).
Describe the Throwable class hierarchy in Java. What are its two main branches and what does each represent?
basics
~20 sThrowable is the root of everything you can throw or catch. It splits into Error (serious system problems you shouldn't catch, like running out of memory) and Exception (problems your program can handle, like a missing file or bad input).
What is the difference between checked and unchecked exceptions, and how does that distinction map onto the Throwable hierarchy?
basics
~10 sChecked exceptions must be handled or declared with throws, or your code won't compile. Unchecked ones (RuntimeException and Error) don't force you to do anything. The compiler enforces checked exceptions; it ignores unchecked ones.
Given an arbitrary throwable class, how do you determine whether it is checked or unchecked?
basics
~10 sLook at its ancestry. If it is a subclass of RuntimeException or of Error, it is unchecked. If it is a subclass of Exception but not of RuntimeException, it is checked.
What is a finally block in Java and when does its code run?
basics
~10 sA finally block holds cleanup code that runs after the try (and any matching catch), whether or not an exception was thrown. You use it to release resources like files or connections.
What is the Java multi-catch clause and how do you write one?
basics
~10 sMulti-catch lets one catch block handle several exception types. You list the types separated by a vertical bar (|), like catch (IOException | SQLException e), and write a single shared body.
What is the try-catch construct in Java, and how does the JVM decide which catch block handles a thrown exception?
basics
~20 sYou put risky code in a try block. If it throws, Java checks each catch block top to bottom and runs the first one whose declared type matches the thrown exception. If none match, the exception keeps propagating up.
What is the try-with-resources statement in Java and what problem does it solve?
basics
~20 sIt is a try block that declares resources (like a file or connection) in parentheses. Java automatically closes them when the block ends, even if an error happens, so you do not need a finally block to close them yourself.
Why must a more specific exception type be caught before its supertype, and what happens if you reverse the order?
basics
~20 sCatch the more specific type first. If you put the supertype (like Exception) before a subtype (like IOException), the subtype's catch can never be reached, so Java refuses to compile it as an unreachable catch.
What happens when a Java method throws an exception that it does not catch?
basics
~20 sIf a method does not catch an exception, Java stops running that method and hands the exception to the method that called it. This repeats up the call chain until some method catches it, or the program prints the error and stops.
How do you rethrow a caught exception in Java while preserving its original stack trace, and what is the difference between rethrowing the same instance and wrapping it in a new exception?
basics
~20 sUse throw e; to rethrow the exact exception you caught. Because it is the same object, its stack trace stays intact. If you wrap it in a new exception, pass the original as the cause so the trace is not lost.
What is the throws clause in Java, and what does the compiler's 'catch-or-declare' rule require?
basics
~10 sThe throws clause lists exceptions a method might throw. For checked exceptions Java forces you to either handle them with try/catch or declare them with throws, so callers know to deal with them.
What does the throw statement do in Java, and what must follow the throw keyword?
basics
~20 sthrow raises an exception. After the keyword you put an object that is a Throwable (an Exception or Error). Once thrown, the current method stops normal execution and control jumps to a matching catch block, or out of the method if none catches it.
What is the difference between `throw` and `throws` in Java?
basics
~20 sthrow is a statement that raises one exception right now inside a method body. throws is part of a method's signature that declares which checked exceptions the method might pass to its caller. One acts; the other warns.
What is exception chaining in Java, and why would you wrap one exception inside another?
basics
~20 sException chaining means attaching the original exception (the cause) to a new exception when you wrap it. You pass the original to the new exception's constructor so you don't lose information about what really went wrong.
When designing a custom Java exception, what is the difference between a checked and an unchecked exception, and how do you decide which one to extend?
basics
~20 sA checked exception (extends Exception) must be declared or caught — the compiler forces it. An unchecked exception (extends RuntimeException) does not. Use checked for problems the caller can recover from, unchecked for programming bugs.
When defining a custom exception in Java, how do you decide whether to extend Exception (checked) or RuntimeException (unchecked)?
basics
~10 sExtend Exception for a checked exception that callers must handle or declare; extend RuntimeException for an unchecked one that needs no try/catch or throws clause. Use checked for recoverable errors, unchecked for programming bugs.
What are the conventional constructors a custom exception class should provide, and how do they delegate to super?
basics
~10 sProvide four constructors mirroring Throwable: no-arg, message-only (String), message-and-cause (String, Throwable), and cause-only (Throwable). Each just calls the matching super(...) so the message and cause are stored correctly.
Explain the 'recoverable condition vs programming error' rule for choosing checked vs unchecked custom exceptions, with examples of each.
basics
~20 sIf the caller can do something useful to recover, make it checked so they're forced to handle it. If the failure is a bug the caller should have prevented, or can't recover from, make it unchecked.
What is an empty catch block, and why is swallowing an exception considered a bug?
basics
~20 sAn empty catch block catches an exception but does nothing with it, so the error silently disappears. The program keeps running as if nothing went wrong, hiding real failures and making bugs very hard to find.
What is the fail-fast principle in Java, and why is validating inputs early considered good practice?
basics
~20 sFail-fast means you check for invalid inputs or bad state right away and stop immediately by throwing an error, instead of letting the program keep running with bad data and breaking later in a confusing place.
What is try-with-resources in Java, and why is it preferred over closing resources in a finally block?
basics
~20 stry-with-resources declares a resource in the try header and Java automatically closes it when the block ends, even on an exception. It is preferred because manual finally blocks are easy to get wrong and can leak resources.
You see this code in a review. What is wrong with it and how would you fix it?
basics
~10 sIt throws a new exception but never passes the original (e) as the cause, so the real error and its stack trace are lost. Fix it by chaining: throw new ServiceException("...", e).
Why should you catch a specific exception type instead of a broad Exception or Throwable?
basics
~20 sCatching a specific type means you only handle the errors you expect and know how to deal with. A broad catch grabs everything — including bugs and errors you never anticipated — and treats them all the same, which hides real problems.