Exception Handling
throws Keyword
Analyze throws signature declarations, exception propagation routes, and overriding variance rules.
Interview: Evaluates rules for checked exception inheritance during method overriding (narrower/fewer allowed, broader/new prohibited).
The throws keyword is declared in a method signature to specify that the method may propagate certain checked exceptions to its callers. It is a key part of Java's handle-or-declare requirement.
Signature Declaration
Acts as a compiler warning. The method signature lists the checked exceptions it may throw, passing handling responsibility to callers.
Overriding Rules
CRITICAL: Overriding methods in subclasses can declare fewer or narrower checked exceptions, but cannot declare broader or new ones.
Checked Exceptions
Only required for checked exceptions. Declaring unchecked exceptions in the throws clause is allowed but optional.
Checked Exception Inheritance Rules
When a subclass overrides a parent method, the compiler enforces strict rules on the throws clause to protect polymorphism:
- Narrower Exceptions: The subclass method can declare fewer or narrower (subclass) checked exceptions than the parent method.
- No Broader Exceptions: The subclass method cannot declare broader or new checked exceptions. Doing so would violate the Liskov Substitution Principle, as callers using a parent reference would receive unhandled exceptions.
- Unchecked Exceptions: The subclass method can declare any unchecked exceptions (e.g.
RuntimeException) without restriction.
Common Pitfalls
- Overriding with Broader Exceptions: Attempting to declare a broader checked exception (e.g.
Exception) in an overridden subclass method, causing a compiler error. - Declaring throws Exception everywhere: Adding
throws Exceptionto all method signatures to bypass compiler checks, which propagates boilerplate code to callers.
Best Practices
- Declare Specific Exceptions: Only declare the specific checked exceptions that the method can throw, rather than general parent classes.
- Document Exceptions: Use the Javadoc
@throwstag to explain the conditions under which each exception is thrown.
Interview-Relevant Information
Q1: If a superclass method throws IOException, can the subclass overriding method throw Exception?
Answer: No. The overriding method cannot throw broader checked exceptions. Since Exception is a superclass of IOException, this would violate inheritance rules and cause a compilation error.
Q2: If a superclass method does not declare any exceptions, can the subclass overriding method throw a checked exception?
Answer: No. If the parent method throws no checked exceptions, the overriding subclass method cannot declare any checked exceptions. It can, however, throw any unchecked exceptions.
Quick Checklist
Can you write a method signature using the throws keyword, list the checked exception rules for subclass overrides, and explain the difference between throw and throws? If yes, you understand the throws keyword.
Use Cases
Declaring potential file access failures in low-level I/O methods to warn calling layers.
Enforcing exception handling requirements in public APIs.
Common Mistakes
Declaring broader checked exceptions in overridden subclass methods, resulting in compile-time errors.
Declaring throws Exception globally in utility APIs to bypass handle-or-declare requirements.