Categories: Java

Exception Handling with Method Overriding in Java

Exception Handling with Method Overriding:

Ambiguity arises when method overriding and exception handling are combined. Which definition should be followed confuses the compiler.

Problem categories:

It has two different kinds of issues, which are as follows:

  • Problem 1: If The SuperClass doesn’t declare an exception
  • Problem 2: If The SuperClass declares an exception
If The SuperClass does not declare an exception:

Two scenarios that will occur in this issue are as follows:

  • Case 1: If a subclass declares a checked exception and the superclass doesn’t
  • Case 2: In the event that SuperClass doesn’t declare any exceptions and SubClass does

Let’s talk about the two cases mentioned above and interpret them using the following examples:

Case 1: If the SuperClass doesn’t declare any exceptions and the checked exceptions are declared by the subclass.

Example:

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");
}

// Driver code
public static void main(String args[]) {
 SuperClass s = new SubClass();
 s.method();
}
}
Output:
void method() throws IOException {
     ^
  overridden method does not throw IOException
1 error

Case 2: In the event that SuperClass doesn’t declare any exceptions and SubClass does

Example:

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 Unchecked Exception ArithmeticException
 void method() throws ArithmeticException
 {

  // ArithmeticException is of type Unchecked Exception
  // so the compiler won't give any error

  System.out.println("SubClass");
 }

 // Driver code
 public static void main(String args[])
 {
  SuperClass s = new SubClass();
  s.method();
 }
}
Output:
SubClass

Now let’s focus on the subsequent issue that arises from that, which occurs if The SuperClass declares an exception. Three cases will present themselves in this issue:

  • Case 1: When a SuperClass declares an exception and a SubClass declares an exception that is not the SuperClass’s declared exception’s child exception.
  • Case 2: When a child exception of a SuperClass declared exception is declared by a SubClass.
  • Case 3: When a SuperClass declares an exception but a SubClass does not.
Example for Case 1:

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 an exception
// which are not a child exception of RuntimeException
void method() throws Exception {

 // Exception is not a child exception
 // of the RuntimeException
 // So the compiler will give an error

 System.out.println("SubClass");
}

// Driver code
public static void main(String args[]) {
 SuperClass s = new SubClass();
 s.method();
}
}
Output:
void method() throws Exception {
     ^
  overridden method does not throw Exception
1 error
Example for Case 2:

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
 {

  // ArithmeticException is a child exception
  // of the RuntimeException
  // So the compiler won't give an error
  System.out.println("SubClass");
 }

 // Driver code
 public static void main(String args[])
 {
  SuperClass s = new SubClass();
  s.method();
 }
}
Output:
SubClass
Example for Case 3:

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");
 }

 // Driver code
 public static void main(String args[])
 {
  SuperClass s = new SubClass();
 try {
  s.method();
 } catch (IOException e) {
  e.printStackTrace();
 }
 }
}
Output:
SubClass

Note: also read about the Interface vs Abstract class

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago