Exception Handling with Method Overriding in Java

An exception occurs when an unexpected event occurs during the execution of a program at runtime and disrupts the normal flow of programming instructions. Exceptions are used to handle errors at runtime, helping to keep the program’s flow constant.

What is Exception Handling with Method Overriding

  • A subclass must follow the method signature specified in the superclass when it overrides a method from that superclass.
  • The exception types that the method may throw are listed here. The overriding method in the subclass must adhere to the same or more particular exception types if the superclass method specifies that it may throw a specific kind of exception.
  • Ambiguity occurs when exception handling is combined with method overriding. The compiler is uncertain which definition to follow.

Types of problems

This raises two different kinds of issues, which are as follows:

Issue 1: Should the SuperClass fail to raise an exception

Issue 2: If the SuperClass makes an exception

Let’s examine different cases related to these problems and observe their outcomes.

Problem 1: If the SuperClass does not throw an exception

In this problem, the following two cases occur:

Case 1: If the SuperClass declares no exception and the subclass declares a checked exception

Case 2: If the SuperClass does not specify an exception and the SubClass specifies an unhandled exception

Case 1: If the SuperClass declares no exception, and the subclass declares a checked exception.

Example:

import java.io.*; class FirstCode { void method() { System.out.println("SuperClass"); } } class SubClass extends FirstCode { void method() throws IOException { System.out.println("SubClass"); } public static void main(String args[]) { FirstCode s = new FirstCode(); s.method(); } }

Output:

FirstCode.java:9: error: method() in SubClass cannot override method() in FirstCode
void method() throws IOException {
^
The overridden method does not throw IOException
1 error

Case 2: If the SuperClass does not specify an exception and the SubClass specifies an unhandled exception

import java.io.*; class SuperClass { void method() { System.out.println("SuperClass"); } } class SubClass extends SuperClass { void method() throws ArithmeticException { System.out.println("SubClass"); } public static void main(String args[]) { SuperClass s = new SubClass(); s.method(); } }

Output:

SubClass

We will now focus on another problem associated with the SuperClass if it throws an exception. In this issue, the following 3 cases occur.

Case 1: If the SuperClass declares an Exception and the Subclass declares exceptions other than the child exception of the SuperClass declared Exception.
Case 2: If a SuperClass declares an Exception and a SubClass declares a child exception of the SuperClass declared Exception.
Case 3: If the SuperClass declares an exception and the SubClass declares no exception.

Exception Handling with Method Overriding in Java

There are several rules to consider when overriding a method using exception handling.

Superclass Method without Exception

If the superclass method does not declare an exception, the subclass method overriding it cannot declare a checked exception. However, unchecked exceptions are allowed to be declared.

Superclass Method with Exception

If the superclass method declares an exception, the subclass method overriding it has a few options:

  • It can declare the same exception type.
  • It can declare a subclass exception.
  • It can declare no exception (not recommended if the superclass declares an exception).

Example:

import java.io.*; class Parent{ void msg() { System.out.println("parent method"); } } public class FirstCode extends Parent{ void msg() throws IOException { System.out.println("FirstCode "); } public static void main(String args[]) { Parent p = new FirstCode (); p.msg(); } }

Output:

FirstCode.java:9: error: msg() in FirstCode cannot override msg() in Parent
void msg() throws IOException {
^
overridden method does not throw IOException
1 error

Example:

import java.io.*; class FirstCode { void msg()throws Exception{ System.out.println("parent method"); } } class Tutorials extends FirstCode{ void msg() { System.out.println("child method"); } public static void main(String args[]){ FirstCode p = new Tutorials(); try { p.msg(); } catch(Exception e) {} } }

Output:

child method

Points to remember

  • The SubClass can only declare unchecked exceptions; the SubClass cannot declare checked exceptions if the SuperClass does not declare any.
  • If the SuperClass declares an exception, the SubClass is limited to declaring either the same exception or its offspring and any additional Runtime Exceptions; it is not permitted to declare any new checked exceptions at the same or higher level.
  • The SubClass may declare without exception if the SuperClass declares one.

Primary reasons why an exception occurs

  • Invalid user input
  • Device failure
  • Loss of network connection
  • Physical limitations (out-of-disk memory)
  • Code errors
  • Opening an unavailable file

How does the programmer handle the exception?

Custom Error Handling

  • The Java error handling is handled with five keywords: attempt, catch, throw, and finally.
  • What it all means is how they work. The try block contains program statements that you believe can throw an exception. Exceptions are thrown if they occur in the try block.
  • Your code can catch this exception using a catch block and handle it in a rational manner.
  • The Java runtime system automatically throws system-generated exceptions. To manually throw an exception, use the keyword “threw.”
  • A Throws clause shall specify, as such, any exception thrown from a method. The last block will contain any code that needs to be run after completion of the attempt block.

Conclusion

Exception handling is a mechanism for dealing with errors and unexpected situations that may arise during a program’s execution.

Method overriding, on the other hand, is a concept in object-oriented programming in which a subclass provides a specific implementation for a method already defined in its superclass.

In summary, exception handling with method overriding involves ensuring that the overriding methods handle exceptions declared by the overridden methods, and it provides an opportunity to customise the exception handling behaviour in the subclass.

Leave a Reply