Exception Handling Rules for Parent and Subclass in Java

❓ Ever wondered why subclass methods can throw runtime exceptions but not new checked ones when overriding a parent method?
You’re not alone. It’s a common confusion among Java learners. But don’t worry — in this post, we’ll simplify it using real-world analogies and code.
👨🏫 The Scenario
You have a parent class with a method that either throws a checked exception, a runtime exception, or nothing at all.
Now, you override that method in a subclass and want to throw some other exception.
Let’s see what’s allowed and what’s not — in Java rules and with real-life examples.
⚖️ Java Rule: Exception Rules for Method Overriding
Parent Throws | Subclass Can Throw? | ✅ or ❌ |
Nothing | ✅ Any runtime (unchecked) exception | ✅ |
Nothing | ❌ Any new checked exception | ❌ |
Checked Exception | ✅ Same or narrower checked exception | ✅ |
Checked Exception | ❌ New/broader checked exception | ❌ |
Anything | ✅ Runtime exceptions (anytime!) | ✅ |
🏫 Analogy 1: Principal and Teacher
Let’s say a Principal (parent class) has a rule:
"If there’s a major issue, submit a report."
(throwsIOException
)
A Teacher (subclass) can:
🔸 Submit the same report (
IOException
) ✅🔹 Do nothing quietly ✅
❌ But they can’t call the police (SQLException) unless Principal allows it
This is just like Java’s checked exception rule — you can only match or reduce, not introduce stricter exceptions.
✅ Example 1: Subclass throws nothing (okay)
class Principal {
public void handleIssue() throws IOException {
System.out.println("Principal files a report");
}
}
class Teacher extends Principal {
@Override
public void handleIssue() {
System.out.println("Teacher handled it quietly");
}
}
Works fine! ✔️
❌ Example 2: Subclass throws new checked exception (not okay)
class Teacher extends Principal {
@Override
public void handleIssue() throws SQLException { // ❌ Not allowed
System.out.println("Teacher escalated it!");
}
}
Compile-time error!
Because SQLException
is a new checked exception not declared in the parent method.
💣 Runtime Exceptions are Always Allowed! (Yes, Really)
🛣 Analogy 2: Driving with Surprise Potholes
Imagine driving (method execution) and suddenly hitting a pothole (NullPointerException).
You didn’t expect it — it’s a runtime error.
Java allows these unchecked exceptions in any method, even if the parent doesn't throw anything.
✅ Example 3: Subclass throws RuntimeException
class Parent {
public void performTask() {
System.out.println("Parent task complete.");
}
}
class Child extends Parent {
@Override
public void performTask() {
System.out.println("Child encountered runtime issue");
throw new NullPointerException("Oops! Something was null.");
}
}
✅ This compiles and runs fine!
Because RuntimeExceptions are unchecked, Java allows them freely in method overrides.
🔁 Final Summary Table
Situation | Allowed? | Why? |
Subclass throws same checked exception | ✅ | Same rule |
Subclass throws subclass of checked | ✅ | Narrower |
Subclass throws broader checked exception | ❌ | Unsafe — compiler error |
Subclass throws runtime exception | ✅ | Always allowed (unchecked) |
Subclass throws nothing | ✅ | Fine |
Diagram: Exception Handling Rules in Method Overriding
+-------------------------+
| Parent Method |
| Throws CheckedException |
+-----------+-------------+
|
| Overrides method can throw:
|
+-------------------------+ +-------------------------+
| Same CheckedException | | No Exception |
+-------------------------+ +-------------------------+
| |
| |
+-------------------------+ +-------------------------+
| Subclass of Checked | | Any RuntimeException |
| (Narrower Exception) | | (Always Allowed) |
+-------------------------+ +-------------------------+
|
|
+-------------------------+
| New or Broader Checked |
| Exception (Not Allowed)|
+-------------------------+
🎯 Takeaway
In Java, your subclass must follow or loosen the exception rules of the parent. But when it comes to runtime exceptions, you’re free to throw them anytime — just like unexpected potholes during a smooth drive!
💡 Pro Tip
When designing APIs:
Use checked exceptions for expected failures (like file not found).
Use runtime exceptions for programming bugs (like null access, illegal arguments).
Did this analogy help you understand exception rules better? 🤔
Let me know in the comments — and follow for more beginner-friendly Java content. 🚀
Subscribe to my newsletter
Read articles from bharath chandanala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
