In Java, checked exceptions play a significant role in defining the contract and behavior of methods during inheritance. we will explore some key rules for using checked exceptions during inheritance, along with code examples in this article.
Method Overriding is possible only if certain conditions are satisfied. These are a few of them.
- The argument list of the overridden method must be the same for both the subclass and superclass.
- Constructors and final-declared methods can’t be overridden.
- Only methods that are inherited and not static can be overridden.
Exception handling while overriding method in Java
Method overriding for exception handling involves two main cases:
- When a superclass does not declare an exception
- When a superclass declares an exception
There are few important points to remember while handling exceptions using method overriding.
- If the superclass method does not declare an exception, then the overriding subclass method cannot declare a checked exception, but it can declare an unchecked exception.
- If the superclass method declares an exception, then the overriding subclass method can declare the same exception, subclass exception or no exception, but it cannot declare a parent exception thrown by the superclass method.
- If the superclass method throws a checked or compile-time exception, then subclass method can throw an exception, which is a subclass of the superclass method’s exception or same exception.
- If an superclass method throws an unchecked or runtime exception, then subclass method can throw any unchecked or runtime exception. Also the subclass method can throw the same exception, which is thrown by the superclass method or even it can ignore the exception declare by super class.
Let’s understand these concept with few example.
1. Sub classes not allowed to add New Checked Exceptions:
Rule: A subclass (i.e, child class) is not allowed to add new checked exceptions to a method’s signature that it declare in its superclass method.
Example:
class Parent {
void doSomething() throws IOException { //Checked Exception
// ...
}
}
class Child extends Parent {
// Error: Child class is adding a new checked exception (SQLException)
void doSomething() throws SQLException {
// ...
}
}
Note: The child class should only throw exceptions that are compatible with the exceptions thrown by the parent class method. In this case, the child class can throw IOException or any of its subclasses.
Example Code Fix:
class Parent {
void doSomething() throws IOException {
// ...
}
}
class Child extends Parent {
void doSomething() {
// OR
// void doSomething() {
// ...
}
}
2. Sub classes are allowed to Narrow the Exception Type:
Rule: A subclass is allowed to narrow the exception type (use a subclass of the parent class’s exception) in the overridden method’s throws clause.
Example Code Fix:
class Parent {
void doSomething() throws IOException {
// ...
}
}
class Child extends Parent {
void doSomething() throws FileNotFoundException {
// OR
// void doSomething() {
// ...
}
}
3. Sub classes May Declare Fewer Checked Exceptions:
Rule: A subclass is allowed to declare fewer checked exceptions (or none) in the overridden method’s throws clause compared to the superclass method. This is often referred to as “exception hiding.”
Example:
class Parent {
void doSomething() throws IOException, SQLException {
// ...
}
}
class Child extends Parent {
// Valid: Child class declares fewer exceptions (only IOException)
void doSomething() throws IOException {
// ...
}
}
Note: Child class ca declares fewer exceptions. This is a valid and safe operation. However, if the child class decides to throw additional exceptions, it must follow rule no. 1.
4. Sub classes May Choose Not to Throw Any Exceptions
Rule: A subclass is allowed to choose not to throw any checked exceptions, even if the superclass method does.
Example:
class Parent {
void doSomething() throws IOException {
// ...
}
}
class Child extends Parent {
void doSomething() {// Valid: Child class can skip throwing any exceptions
// ...
}
}
5. Sub classes Cannot Throw Broader Exceptions
Rule: A subclass is not allowed to throw broader exceptions (use a superclass of the parent class’s exception) in the overridden method’s throws clause.
Example:
import java.io.IOException;
class Parent {
void doSomething() throws IOException {
// ...
}
}
class Child extends Parent {
// Valid: Child class narrows the exception to a subclass (e.g., FileNotFoundException)
void doSomething() throws FileNotFoundException {
// ...
}
}
6. Sub classes can throw any unchecked exception.
Rule: A subclass is allowed to throw any unchecked exceptions, If SuperClass does not declare an exception.
Example:
class Parent {
void doSomething() { //If SuperClass does not declare an exception
// ...
}
}
class Child extends Parent {
void doSomething() throws NullPointerException{ //only unchecked exceptions allowed
// ...
}
}
Conclusion
- If SuperClass does not declare an exception, then the SubClass can only declare unchecked exceptions, but not the checked exceptions.
- If SuperClass declares an exception, then the SubClass can only declare the same or child exceptions of the exception declared by the SuperClass and any new Runtime Exceptions, just not any new checked exceptions at the same level or higher.
- If SuperClass declares an exception, then the SubClass can declare without exception.
1 thought on “Java exception handling mechanism in details”