1. Is a try-finally block valid without a catch?
You can use try-finally to guarantee cleanup when you don’t need to handle the exception locally. The exception propagates after finally runs.
Get the Preplance app for a seamless learning experience. Practice offline, get daily streaks, and stay ahead with real-time interview updates.
Get it on
Google Play
4.9/5 Rating on Store
Java · Question Set
Exceptions & Error Handling in Java interview questions for placements and exams.
Questions
14
Included in this set
Subject
Java
Explore more sets
Difficulty
Mixed
Level of this set
Go through each question and its explanation. Use this set as a focused practice pack for Java.
You can use try-finally to guarantee cleanup when you don’t need to handle the exception locally. The exception propagates after finally runs.
For complete preparation, combine this set with full subject-wise practice for Java. You can also explore other subjects and sets from the links below.
finally runs after try or catch for cleanup, regardless of normal or exceptional flow. It can be skipped in rare cases like System.exit, JVM crash, or power loss.
Catch blocks are matched top-down. If a broader type comes first, narrower types are unreachable, causing a compile-time error.
If both the try body and close() throw, the try body’s exception is primary, and close() exceptions are attached as suppressed, retrievable via getSuppressed().
Create a checked exception to force callers to acknowledge a recoverable domain error—like a business rule violation or a missing required configuration. Create an unchecked exception when the failure indicates a programmer mistake or an unexpected state that typical callers cannot handle—like illegal arguments, null misuses, or invariant violations. Use descriptive names and include context in the message.
Errors like OutOfMemoryError or StackOverflowError signal serious conditions. They are unchecked and not intended for normal recovery logic.
Exception translation means catching a low-level exception and throwing a higher-level, more meaningful one. It’s appropriate when moving across layers—for example, converting SQLException into a domain-specific DataAccessException. Preserve the cause using the constructor so the original stack trace remains available.
Throwable is the root. It has two direct children: Error and Exception. RuntimeException extends Exception. Errors are siblings of Exception, not children of RuntimeException.
One: Fail fast with clear messages—include context like IDs and key parameters for quick diagnosis. Two: Do not swallow exceptions—either handle meaningfully or rethrow; always log with level and cause. Three: Use domain-specific exceptions for business errors, and keep stack traces for unexpected faults; map them to proper HTTP or API error codes for clients.
Throwing and capturing exceptions involves filling stack traces and unwinding frames. Use exceptions for exceptional cases, not routine branching.
Use @ControllerAdvice with @ExceptionHandler methods to map exceptions to structured error responses. Return a stable JSON envelope with fields like timestamp, error code, message, and correlation ID. Map domain errors to 4xx with actionable messages; map unexpected faults to 5xx with a generic message and a trace ID. Always log server-side with the cause and context; never leak sensitive details in responses.
Checked exceptions must be declared or handled; examples include IOException and SQLException. Unchecked exceptions extend RuntimeException; NullPointerException and ArithmeticException are typical. Errors are also unchecked and indicate serious VM problems.
try-with-resources auto-closes resources that implement AutoCloseable (or Closeable which extends it). This ensures deterministic cleanup even when exceptions occur.
Use throws in the method signature to advertise possible exceptions; use throw inside code to create and raise a specific exception instance.