throws Keyword
The throws keyword in Java is used in a method signature to declare exceptions that a method might pass to its caller. It shifts the responsibility of handling the exception to the calling method. This is a high-frequency interview topic, often confused with throw.
What Is the throws Keyword?
- Used to declare possible exceptions
- Appears in the method signature
- Does not handle exceptions
- Transfers exception handling responsibility to the caller
returnType methodName() throws ExceptionType {
// method body
}
Why throws Is Needed
- Inform callers about possible failures
- Enforce handling of checked exceptions
- Support exception propagation
- Keep method logic clean
Basic Example (Checked Exception)
void readFile() throws IOException {
FileReader fr = new FileReader("data.txt");
}
✔ Compiler forces caller to handle or declare IOException
Handling a Method That Uses throws
Option 1: Handle Using try-catch
try {
readFile();
} catch (IOException e) {
System.out.println("File error");
}
Option 2: Declare Using throws
void process() throws IOException {
readFile();
}
✔ Exception propagates upward
throws with Multiple Exceptions
void process() throws IOException, SQLException {
// code
}
✔ Multiple exceptions separated by commas
throws with Unchecked Exceptions
void test() throws ArithmeticException {
int x = 10 / 0;
}
✔ Allowed but not required
⚠️ Usually unnecessary
throws and Method Overriding (Important Rule)
An overriding method:
- Cannot throw broader checked exceptions
- Can throw same or subclass exceptions
- Can remove exception entirely
class A {
void show() throws IOException { }
}
class B extends A {
void show() throws FileNotFoundException { } // ✔ allowed
}
throws vs throw (Interview Favorite)
| Aspect | throws | throw |
|---|---|---|
| Used in | Method signature | Method body |
| Purpose | Declares exceptions | Throws exception |
| Number allowed | Multiple | One at a time |
| Handles exception | ❌ No | ❌ No |
| Role | Inform caller | Create exception |
Exception Propagation Using throws
void a() throws IOException {
b();
}
void b() throws IOException {
c();
}
void c() throws IOException {
throw new IOException();
}
✔ Exception flows up the call stack
✔ Must be handled at some level
When to Use throws
- When method cannot handle the exception meaningfully
- When exception is part of method contract
- In low-level methods (DAO, IO, network)
When NOT to Use throws
- When exception can be handled locally
- When hiding exceptions harms readability
- Declaring unnecessary unchecked exceptions
Common Beginner Mistakes
- Confusing throw and throws
- Declaring throws Exception everywhere
- Forgetting to handle propagated exceptions
- Declaring unchecked exceptions unnecessarily
Interview-Ready Answers
Short Answer
The throws keyword is used to declare exceptions that a method may pass to its caller.
Detailed Answer
In Java, the throws keyword is used in a method declaration to specify exceptions that the method does not handle itself. It informs the caller about potential exceptions and enforces handling of checked exceptions through try-catch or further propagation.
Key Takeaway
throws declares responsibility, it does not handle errors. Use it to create clean method contracts and support controlled exception propagation.