Exception Handling with Method Overriding in Java

There are few things to remember when overriding a method with exception handling. If super class method does not declare any exception, then sub class overridden method cannot declare checked exception but it can declare unchecked exceptions.

There are many rules if we talk about methodoverriding with exception handling.
If the superclass method does not declare an exception

If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.

If the superclass method declares an exception

If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

Note: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception.

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


Output:
Compile Time Error

Note: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but can declare unchecked exception.

import java.io.*; class Test { void msg(){System.out.println("Test");} } class TestExceptionChild1 extends Test{ void msg()throws ArithmeticException{ System.out.println("child"); } public static void main(String args[]){ Test p=new TestExceptionChild1(); p.msg(); } }


Output:
child
If the superclass method declares an exception

Note: If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

Example in case subclass overridden method declares parent exception
import java.io.*; class Test1 { void msg()throws ArithmeticException{System.out.println("parent");} } class TestExceptionChild2 extends Test1{ void msg()throws Exception{System.out.println("child");} public static void main(String args[]){ Test1 p=new TestExceptionChild2(); try{ p.msg(); }catch(Exception e){} } } import java.io.*; class Test { void msg(){System.out.println("Test");} } class TestExceptionChild1 extends Test{ void msg()throws ArithmeticException{ System.out.println("child"); } public static void main(String args[]){ Test p=new TestExceptionChild1(); p.msg(); } }


Output:
Compile Time Error
Example in case subclass overridden method declares same exception
import java.io.*; class Test2 { void msg()throws Exception{System.out.println("parent");} } class Test1 extends Test2{ void msg()throws Exception{System.out.println("child");} public static void main(String args[]){ Test2 p=new TestExceptionChild3(); try{ p.msg(); }catch(Exception e){} } }


Output:
child
Example in case subclass overridden method declares subclass exception
import java.io.*; class Test { void msg()throws Exception{System.out.println("parent");} } class Test1 extends Test{ void msg()throws ArithmeticException{System.out.println("child");} public static void main(String args[]){ Test p=new Test1(); try { p.msg(); }catch(Exception e){} } }


Output:
child
Example in case subclass overridden method declares no exception
import java.io.*; class Test { void msg()throws Exception{System.out.println("parent");} } class Test1 extends Test{ void msg(){System.out.println("child");} public static void main(String args[]){ Test p=new Test1(); try { p.msg(); }catch(Exception e){} } }


Output:
child
Example of Subclass overriden Method declaring Checked Exception
import java.io.*; class Super { void show() { System.out.println("parent class"); } } public class Test extends Super { void show() throws IOException //Compile time error { System.out.println("parent class"); } public static void main( String[] args ) { Super s=new Test(); s.show(); } }


Output:
Compile time error

As the method show() doesn't throw any exception while in Super class, hence its overridden version also cannot throw any checked exception.

Example of Subclass overriden Method declaring Unchecked Exception
import java.io.*; class Super { void show(){ System.out.println("parent class"); } } public class Test extends Super { void show() throws ArrayIndexOutOfBoundsException //Correct { System.out.println("child class"); } public static void main(String[] args) { Super s=new Test(); s.show(); } }


Output:
child class
More about Overriden Methods and Exceptions

If Super class method throws an exception, then Subclass overriden method can throw the same exception or no exception, but must not throw parent exception of the exception thrown by Super class method.

It means, if Super class method throws object of NullPointerException class, then Subclass method can either throw same exception, or can throw no exception, but it can never throw object of Exception class (parent of NullPointerException class).

Example of Subclass overriden method with same Exception
import java.io.*; class Super { void show() throws Exception { System.out.println("parent class"); } } public class Test extends Super { void show() throws Exception //Correct { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Test(); s.show(); } catch(Exception e){} } }


Output:
child class
Example of Subclass overriden method with no Exception
import java.io.*; class Super { void show() throws Exception { System.out.println("parent class"); } } public class Test extends Super { void show() //Correct { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Test(); s.show(); } catch(Exception e){} } }


Output:
child class
Example of Subclass overriden method with parent Exception
import java.io.*; class Super { void show() throws ArithmeticException { System.out.println("parent class"); } } public class Test extends Super { void show() throws Exception //Cmpile time Error { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Test(); s.show(); } catch(Exception e){} } }


Output:
Cmpile time Error



Instagram