Exception Handling with Method Overriding in Java
Prerequisite: Exception and Exception Handling in Java, Overriding in Java, Checked and Unchecked Exception.
An Exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at run-time, that disrupts the normal flow of the program’s instructions. Exception handling are used to handle runtime error. It helps to maintain the normal flow of the program.
In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
Exception Handling with Method Overriding
When Exception handling is involved with Method overriding, ambiguity occurs. The compiler gets confused as which definition is to be followed. Such problems were of two types:
- Problem 1: If The SuperClass doesn’t declare an exception:
In this problem, two cases arise:
- Case 1: If SuperClass doesn’t declare any exception and subclass declare checked exception
Example:
importjava.io.*;classSuperClass {// SuperClass doesn't declare any exceptionvoidmethod(){System.out.println("SuperClass");}}// SuperClass inherited by the SubClassclassSubClassextendsSuperClass {// method() declaring Checked Exception IOExceptionvoidmethod()throwsIOException{// IOException is of type Checked Exception// so the compiler will give ErrorSystem.out.println("SubClass");}// Driver codepublicstaticvoidmain(String args[]){SuperClass s =newSubClass();s.method();}}chevron_rightfilter_noneCompile Errors:
prog.java:16: error: method() in SubClass cannot override method() in SuperClass void method() throws IOException ^ overridden method does not throw IOException 1 error - Case 2: If SuperClass doesn’t declare any exception and SubClass declare Unchecked exception
Example:
importjava.io.*;classSuperClass {// SuperClass doesn't declare any exceptionvoidmethod(){System.out.println("SuperClass");}}// SuperClass inherited by the SubClassclassSubClassextendsSuperClass {// method() declaring Unchecked Exception ArithmeticExceptionvoidmethod()throwsArithmeticException{// ArithmeticException is of type Unchecked Exception// so the compiler won't give any errorSystem.out.println("SubClass");}// Driver codepublicstaticvoidmain(String args[]){SuperClass s =newSubClass();s.method();}}chevron_rightfilter_noneOutput:
SubClass
- Case 1: If SuperClass doesn’t declare any exception and subclass declare checked exception
- Problem 2: If The SuperClass declares an exception:
In this problem also, two cases arise:
- Case 1: If SuperClass declares an exception and SubClass declares exceptions other than the child exception of the SuperClass declared Exception
Example:
importjava.io.*;classSuperClass {// SuperClass declares an exceptionvoidmethod()throwsRuntimeException{System.out.println("SuperClass");}}// SuperClass inherited by the SubClassclassSubClassextendsSuperClass {// SubClass declaring an exception// which are not a child exception of RuntimeExceptionvoidmethod()throwsException{// Exception is not a child exception// of the RuntimeException// So the compiler will give an errorSystem.out.println("SubClass");}// Driver codepublicstaticvoidmain(String args[]){SuperClass s =newSubClass();s.method();}}chevron_rightfilter_noneCompile Errors:
prog.java:16: error: method() in SubClass cannot override method() in SuperClass void method() throws Exception ^ overridden method does not throw Exception 1 error - Case 2: If SuperClass declares an exception and SubClass declares an child exception of the SuperClass declared Exception.
Example:
importjava.io.*;classSuperClass {// SuperClass declares an exceptionvoidmethod()throwsRuntimeException{System.out.println("SuperClass");}}// SuperClass inherited by the SubClassclassSubClassextendsSuperClass {// SubClass declaring a child exception// of RuntimeExceptionvoidmethod()throwsArithmeticException{// ArithmeticException is a child exception// of the RuntimeException// So the compiler won't give an errorSystem.out.println("SubClass");}// Driver codepublicstaticvoidmain(String args[]){SuperClass s =newSubClass();s.method();}}chevron_rightfilter_noneOutput:
SubClass
- Case 1: If SuperClass declares an exception and SubClass declares exceptions other than the child exception of the SuperClass declared Exception
Conclusion for Handling such Exceptions: Hence, following conclusions can be derived from the above examples:
- 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 child exceptions of the exception declared by the SuperClass, but not any other exception.
Recommended Posts:
- Java | Exception Handling | Question 2
- Java | Exception Handling | Question 8
- Java | Exception Handling | Question 7
- Java | Exception Handling | Question 6
- Java | Exception Handling | Question 4
- Java | Exception Handling | Question 8
- Java | Exception Handling | Question 1
- Comparison of Exception Handling in C++ and Java
- Java | Exception Handling | Question 3
- Nested try blocks in Exception Handling in Java
- Output of Java program | Set 12(Exception Handling)
- Exception handling in JSP
- Overriding equals method in Java
- Method Overriding with Access Modifier
- Overriding in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



