In this article, we will discuss the rules for exception handling when method is overridden in the child-class from parent-class
Before discussing rules for exception handling one should have good knowledge about,
- Method overriding concept in Java
- Relationship between different types of Exception or Exception hierarchy
- Difference between checked and unchecked exception
- throws keyword in Java
1. Different cases for Method Overriding:
We will consider different cases for discussing rules for exception handling w.r.t method overriding
- If parent-class method doesn’t declare any exception
- If parent-class method declares unchecked exception
- If parent-class method declares checked exception
- If parent-class method declares both checked & unchecked exceptions
2. Rules for Exception handling w.r.t Method Overriding:
- Let us see each case with an example
Rule 1: If parent-class method doesn’t declare any exception
- Then child-class overriding-method can declare any type of unchecked exception
Note: this is the only possibility - If child-class overriding-method declares checked-exception, then compiler throws compile-time error stating
CTE – “Exception IOException is not compatible with throws clause in ParentClass.testMethod()” - Then child-class overriding-method can declare no exception in the overriding-method of child-class
This is very much same as that of overridden-method of parent-class (exactly same method-signature)
Rule 2: If parent-class method declares unchecked–exception
- Then child-class overriding-method can declare any type of unchecked exception
Not necessarily same exception as that of parent-class’ method (only for unchecked exception) - If child-class overriding-method declares any checked-exception, then compiler throws compile-time error stating
CTE – “Exception IOException is not compatible with throws clause in ParentClass.testMethod()” - Then child-class overriding-method can declare no exception in the overriding-method of child-class
Rule 3: If parent-class method declares checked exception
- Then child-class overriding-method can declare any type of unchecked exception
- Then child-class overriding-method can declare same type of checked exception or one of its sub-class or no exception
OR, sub-type of declared checked exception
- Then child-class overriding-method can declare no exception in the overriding-method of child-class
Rule 4: If parent-class method declares combination of both checked & unchecked exceptions
- Then child-class overriding-method can declare any type of unchecked exception
- Then child-class overriding-method can declare same type of checked-exception or one of its sub-class or no exception
- Then child-class overriding-method can declare no exception in the overriding-method of child-class
3. Conclusion:
- When parent-class method declares no exception, then child-class overriding-method can declare,
- No exception
- Any number of unchecked exception
- But checked exception allowed
- When parent-class method declares unchecked exception, then child-class overriding-method can declare,
- No exception
- Any number of unchecked exception
- But checked exception allowed
- When parent-class method declares checked exception, then child-class overriding-method can declare,
- No exception
- Same checked exception
- Sub-type of checked exception
- Any number of unchecked exception
- All above conclusion hold true, even if combination of both checked & unchecked exception is declared in parent-class‘ method
Related Articles:
- Java – Exception Handling
- Java – Exception Hierarchy
- Java – 5 important keywords in Java Exception handling
- Java – Runtime mechanism, what happens when exception is thrown ?
- Java – Checked Exception v/s Unchecked Exception
- Java – Exception propagation
- Java – try-catch block
- Java – finally block
- Java – try with multiple catch blocks
- Java – Nested try-catch block
- Java – Returning value from method having try-catch-finally blocks
- Java – return statement with finally block
- Java – final v/s finally v/s finalize
- Java – Various methods to print exception information
- Java – throw keyword
- Java – throws keyword
- Java – throw v/s throws
- Java – Difference between throws clause and try-catch-finally block
- Java – Rules for Exception handling w.r.t Method Overriding
- Java – User-defined or Custom exception
- Java – Difference between ClassNotFoundException v/s NoClassDefFoundError
- Java – Top Exception and Error
- Java – Interview question and answers on Exception Handling
- Java 7 – try with resources
- Java 7 – multi-catch block
References:
- https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/try.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
- https://docs.oracle.com/javase/tutorial/essential/exceptions/
- https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/Error.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/ArithmeticException.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html
- http://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html
- http://www.oracle.com/technetwork/java/effective-exceptions-092345.html
- http://otfried.org/courses/cs206/slides/slides-stackframes.pdf
Happy Coding !!
Happy Learning !!