Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Widening exception of overridden method in Java

Tags:

java

exception

Assume we have two classes:

Class A:

import java.io.IOException;

public class A  {
    public void test() throws IOException{
        System.out.println("test in A");
    }
}

Class B:

  import java.io.IOException;

  public class B extends A {
      @Override
      public void test() throws Exception{
          System.out.println("test in B");
      }
 }

This gives a compiler error, and I would like to know the reason for it. I can get the answer by myself, but this is not fully scientific, but partly logically.

I wrote a blog post in Azerbaijani. When I wrote the blog I was stuck in the loading process.

Please be careful in quotes:

I think that when the compiler reads the B class, it loads the method headers of A and method headers of B. And when you call test of A, the JVM calls the test of A, but as body calls test of B, and at that time we will have this method:

 public void test() throws IOException{ // <-- Header of A
    System.out.println("test in B"); // <-- Body of B
    // Here I can throw wide Exception from IOException
    // because here is the body of the test in B. The test
    // method in B can throw Exception so the compiler
    // doesn't approve of this version of the code.
 }

Is the process really going on as what I wrote above?

Loading headers issue I was stuck exactly here. How does the linking process work? I can't figure out background of

A a = new B(); // When the compiler converts this line into bytecode
//                does it loads of method headers of A and method
//                body's of B
a.test()

calls the test of the B class. I know logically, but I can't figure out at the compiler level, linking process.

like image 343
Sarkhan Avatar asked Apr 20 '15 10:04

Sarkhan


People also ask

Can we change exception in overridden method?

Overridden methods can throw Exceptions, so long as the method being overridden also throws the same Exceptions. You can't introduce new Exceptions.

Is superclass method throws an exception then overridden method of subclass can throw?

No, you cannot. The sub-class method cannot throw any checked exception not covered by the throws clause of the base super class method.

Can subclass overriding method declare an exception if parent class method doesn't throw an exception?

Conclusion: When parent-class method declares no exception, then child-class overriding-method can declare, No exception.


1 Answers

Imaigine you have the following code:

A a = new B();
try { 
  a.test();
} catch (IOExceoption e) { 
   //do some specific handle for IOExceoption
}

Now imaigine what happens if b.test() throw an Exception which is NOT IOException? nobody will handle it, and that breaks java's checked exceptions mechanism.


The other way around however, is perfectly fine:

public class A  {
     public void test() throws Exception {
        System.out.println("test in A");
    }
}
  public   class B extends A {
      @Override
      public  void test() throws IOException{
          System.out.println("test in B");
      } 
 }

A a = new B();
try { 
  a.test();
} catch (Exception e) { 
   //handle
}

Now, note that the catch handles a general Exception, including the specific IOException, and the code will compile perfectly.

like image 170
amit Avatar answered Nov 11 '22 02:11

amit