Detailed explanation
In Java, checked and unchecked exceptions differ mainly in whether the compiler enforces handling or declaration. Checked exceptions are Exception subclasses other than RuntimeException and its subclasses; examples include IOException and SQLException. Unchecked exceptions commonly refer to RuntimeException subclasses, such as NullPointerException, IllegalArgumentException, and ArrayIndexOutOfBoundsException. Strictly, Java also classifies Error and its subclasses as unchecked. Both categories represent conditions that occur at runtime: “checked” describes compile-time verification, not when an exception happens.
For a checked exception that can escape a method, Java requires the method either to catch it or declare it in a throws clause. This is the catch-or-declare rule. In the example, constructing FileReader can throw FileNotFoundException, a checked subclass of IOException, so declaring throws IOException satisfies the compiler. The caller must then handle or further declare that possibility. Declaring throws does not handle the failure; it communicates that responsibility to callers. Also, an opened FileReader should normally be closed with try-with-resources, which handles cleanup automatically.
Unchecked exceptions have no catch-or-declare requirement. Assigning arr[5] when arr has length 2 compiles, but the JVM’s runtime bounds check throws ArrayIndexOutOfBoundsException. Such exceptions often indicate violated assumptions, invalid arguments, or programming defects, although that is a convention rather than a universal rule. Checked exceptions usually make an anticipated failure visible in an API contract, especially when callers may reasonably respond. Neither category guarantees recoverability: the appropriate response depends on the operation and application context.
At runtime, both kinds follow the same propagation mechanism: Java searches for a compatible catch block as the call stack unwinds. If no handler is found, the thread terminates after uncaught-exception processing; other threads may continue. Interviewers usually assess whether you understand the hierarchy, the compiler’s role, and sensible handling choices. Catch exceptions where you can recover, add useful context, or translate them into an appropriate abstraction. Avoid swallowing failures or catching broadly merely to make code compile.
Key points to remember
- Checked exceptions require catch-or-declare; unchecked exceptions do not.
- Checked exception classes extend Exception but are outside the RuntimeException hierarchy.
- RuntimeException and Error hierarchies are unchecked; errors are generally not intended for routine recovery.
- IOException is checked; NullPointerException and ArrayIndexOutOfBoundsException are unchecked.
- throws declares possible propagation, while throw actually throws an exception object.
Common mistakes candidates make
- Saying checked exceptions occur at compile time rather than explaining compile-time enforcement.
- Assuming a throws declaration handles the exception or automatically performs resource cleanup.
- Treating all checked exceptions as recoverable and all unchecked exceptions as unrecoverable.
Likely follow-up questions
When should a custom exception be checked or unchecked?
Extend Exception when callers should be required to explicitly address a failure through handling or declaration. Extend RuntimeException when mandatory handling would not improve the API, such as for a violated precondition; follow the surrounding API’s conventions.
Can an overriding method declare additional checked exceptions?
An overriding method cannot add checked exceptions broader than those permitted by the overridden method. It may declare narrower checked exceptions, omit them, or declare unchecked exceptions.
Can unchecked exceptions be caught?
Yes, unchecked exceptions can be caught with try-catch just like checked exceptions. Catch them when you have a meaningful recovery or boundary-handling strategy, rather than using catch blocks to hide programming defects.
Interview tip: Start with the compiler-enforcement distinction, give one example of each category, and explain how catch-or-declare affects the caller.
