Last Updated : 02 Aug, 2025
Exception handling with method overriding in Java refers to the rules and behavior that apply when a subclass overrides a method from its superclass and both methods involve exceptions. It ensures that the overridden method in the subclass does not declare broader or new checked exceptions than those declared by the superclass method, maintaining consistency and type safety during runtime execution
Rules for Exception Handling with Method OverridingExample: Method Overriding with Unchecked Exception
Java
// Superclass without exception declaration
class SuperClass {
void method() {
System.out.println("SuperClass method executed");
}
}
// Subclass declaring an unchecked exception
class SubClass extends SuperClass {
@Override
void method() throws ArithmeticException {
System.out.println("SubClass method executed");
throw new ArithmeticException("Exception in SubClass");
}
public static void main(String[] args) {
SuperClass s = new SubClass();
try {
s.method();
} catch (ArithmeticException e) {
System.out.println("Caught Exception: " + e.getMessage());
}
}
}
SubClass method executed Caught Exception: Exception in SubClass
Explanation: In this example, the SuperClass method does not declare any exceptions. The SubClass method overrides the method() and declares an unchecked exception i.e. the ArithmeticException. The main method demonstrates how the exception is thrown and caught in the SubClass.
Method Overriding with Checked Exception RulesWhen exception handling is involved with method overriding, ambiguity occurs. The compiler gets confused as to which definition is to be followed.
There are two types of problems associated with it which are as follows:
Let us discuss different cases under these problems and perceived their outputs.
Problem 1: Superclass Doesn't Declare an ExceptionIn this problem, two cases that will arise are as follows:
Case 1: If Superclass doesn't declare any exception and subclass declare checked exception.
Java
import java.io.*;
class SuperClass {
// SuperClass doesn't declare any exception
void method() {
System.out.println("SuperClass");
}
}
// SuperClass inherited by the SubClass
class SubClass extends SuperClass {
// method() declaring Checked Exception IOException
void method() throws IOException {
// IOException is of type Checked Exception so the compiler will give Error
System.out.println("SubClass");
}
public static void main(String args[]) {
SuperClass s = new SubClass();
s.method();
}
}
Output:
Explanation: A subclass cannot introduce a new checked exception if the superclass method does not declare any.
Case 2: If Superclass doesn't declare any exception and Subclass declare unchecked exception.
Java
class SuperClass {
// SuperClass doesn't declare any exception
void method()
{
System.out.println("SuperClass");
}
}
// SuperClass inherited by the SubClass
class SubClass extends SuperClass {
// method() declaring Unchecked Exception ArithmeticException
void method() throws ArithmeticException
{
// ArithmeticException is of type Unchecked Exception
System.out.println("SubClass");
}
public static void main(String args[])
{
SuperClass s = new SubClass();
s.method();
}
}
Explanation: ArithmeticException is unchecked, so compiler allows it.
Problem 2: Superclass Declares an ExceptionIf the Superclass declares an exception. In this problem 3 cases will arise as follows:
Case 1: If Superclass declares an exception and Subclass declares exceptions other than the child exception of the Superclass declared exception.
Java
class SuperClass {
void method() throws RuntimeException {
System.out.println("SuperClass");
}
}
// Subclass declares an unrelated exception
class SubClass extends SuperClass {
@Override
void method() throws Exception {
// Exception is not a child of RuntimeException
System.out.println("SubClass");
}
public static void main(String[] args) {
SuperClass o = new SubClass();
o.method();
}
}
Output:
Case 2: If Superclass declares an exception and Subclass declares a child exception of the Superclass declared exception.
Java
import java.io.*;
class SuperClass {
// SuperClass declares an exception
void method() throws RuntimeException
{
System.out.println("SuperClass");
}
}
// SuperClass inherited by the SubClass
class SubClass extends SuperClass {
// SubClass declaring a child exception of RuntimeException
void method() throws ArithmeticException {
System.out.println("SubClass");
}
public static void main(String args[])
{
SuperClass s = new SubClass();
s.method();
}
}
Case 3: If Superclass declares an exception and Subclass declares without exception.
Java
import java.io.*;
class SuperClass {
// SuperClass declares an exception
void method() throws IOException
{
System.out.println("SuperClass");
}
}
// SuperClass inherited by the SubClass
class SubClass extends SuperClass {
// SubClass declaring without exception
void method()
{
System.out.println("SubClass");
}
public static void main(String args[])
{
SuperClass s = new SubClass();
try {
s.method();
} catch (IOException e) {
e.printStackTrace();
}
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4