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.
Examples and Interview Scenarios
1. Basic Use of throws (Checked Exception)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
class Demo {
static void readFile() throws FileNotFoundException {
new FileInputStream("data.txt");
}
public static void main(String[] args) throws FileNotFoundException {
readFile();
}
}
Explanation
- Method declares responsibility to caller
- Compiler is satisfied
2. Handling throws Exception Using try-catch
import java.io.FileInputStream;
import java.io.FileNotFoundException;
class Demo {
static void readFile() throws FileNotFoundException {
new FileInputStream("data.txt");
}
public static void main(String[] args) {
try {
readFile();
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
}
}
Output
File not found
3. Multiple Exceptions in throws
import java.io.IOException;
import java.sql.SQLException;
class Demo {
static void process() throws IOException, SQLException {
throw new IOException();
}
public static void main(String[] args) throws IOException, SQLException {
process();
}
}
Explanation
- Multiple checked exceptions can be declared
4. throws with Unchecked Exception (Optional)
class Demo {
static void test() throws ArithmeticException {
int a = 10 / 0;
}
public static void main(String[] args) {
test();
}
}
Explanation
- throws is optional for unchecked exceptions
5. Exception Propagation Using throws
class Demo {
static void m1() throws Exception {
throw new Exception("Error");
}
static void m2() throws Exception {
m1();
}
public static void main(String[] args) {
try {
m2();
} catch (Exception e) {
System.out.println("Handled in main");
}
}
}
Output
Handled in main
6. throws in Method Overriding (Same Exception)
import java.io.IOException;
class A {
void show() throws IOException {}
}
class B extends A {
void show() throws IOException {}
}
Explanation
- Same checked exception allowed
7. throws in Method Overriding (Narrower Exception)
import java.io.IOException;
import java.io.FileNotFoundException;
class A {
void show() throws IOException {}
}
class B extends A {
void show() throws FileNotFoundException {}
}
Explanation
- Subclass of checked exception is allowed
8. Removing throws in Overridden Method
import java.io.IOException;
class A {
void show() throws IOException {}
}
class B extends A {
void show() {} // allowed
}
Explanation
- Child can remove checked exception
9. Widening Checked Exception (Not Allowed)
import java.io.IOException;
class A {
void show() throws IOException {}
}
class B extends A {
// void show() throws Exception {} // ❌ compile-time error
}
Explanation
- Cannot widen checked exception
10. throws with Constructor
import java.io.IOException;
class Demo {
Demo() throws IOException {
throw new IOException("Constructor error");
}
public static void main(String[] args) throws IOException {
new Demo();
}
}
11. throws in Interface Method
import java.io.IOException;
interface Service {
void process() throws IOException;
}
class Impl implements Service {
public void process() throws IOException {
throw new IOException();
}
}
Explanation
- Implementing class must respect interface contract
12. Interface Implementation Removing throws
import java.io.IOException;
interface Service {
void process() throws IOException;
}
class Impl implements Service {
public void process() {} // allowed
}
13. throws with main Method
import java.io.FileNotFoundException;
class Demo {
public static void main(String[] args) throws FileNotFoundException {
throw new FileNotFoundException();
}
}
Explanation
- JVM handles exception if uncaught
14. throws with Custom Checked Exception
class MyException extends Exception {
MyException(String msg) {
super(msg);
}
}
class Demo {
static void test() throws MyException {
throw new MyException("Custom error");
}
public static void main(String[] args) throws MyException {
test();
}
}
15. throws vs Handling (Design Difference)
class Demo {
static void risky() throws Exception {
throw new Exception();
}
static void safe() {
try {
risky();
} catch (Exception e) {
System.out.println("Handled");
}
}
public static void main(String[] args) {
safe();
}
}
Output
Handled
16. throws with RuntimeException
class Demo {
static void test() throws RuntimeException {
throw new RuntimeException("Error");
}
public static void main(String[] args) {
test();
}
}
Explanation
- Legal but unnecessary
17. throws with Method Returning Value
import java.io.IOException;
class Demo {
static int read() throws IOException {
throw new IOException();
}
public static void main(String[] args) throws IOException {
read();
}
}
18. throws and Lambda (Checked Exception Not Allowed Directly)
interface Task {
void run();
}
class Demo {
public static void main(String[] args) {
Task t = () -> {
// throw new IOException(); // ❌ not allowed
throw new RuntimeException();
};
t.run();
}
}
Explanation
- Lambda cannot throw checked exception unless interface allows it
19. Common Interview Trap
class Demo {
static void test() throws Exception {
throw new Exception();
}
public static void main(String[] args) {
// test(); // ❌ compile-time error
}
}
Explanation
- Caller must handle or declare
20. Interview Summary – throws Keyword
class Demo {
static void process() throws Exception {
throw new Exception("Failure");
}
public static void main(String[] args) {
try {
process();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Key Points
- Declares exception to caller
- Mainly for checked exceptions
- Supports exception propagation
- Does not throw exception itself
Output
Failure
Key Takeaway
throws declares responsibility, it does not handle errors. Use it to create clean method contracts and support controlled exception propagation.