coderz.py

Keep Coding Keep Cheering!

Exception Handling with Method Overriding in Java

java thread class
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

Leave a Comment

Advertisement