0

When my Master class throws a checked exception , shouldn't the override method should also implement the checked exception??

class Master{
    String doFileStuff() throws FileNotFoundException{
        return "a";
    }
}

public class test extends Master{

    public static void main(String[] args){
    }


    String doFileStuff(){
        return "a";
    }

}   
user1050619
  • 19,822
  • 85
  • 237
  • 413

5 Answers5

0

The override method should have preserve the same contract.

Basically that means it can throw a FileNotFoundException or a list of subclasses of FileNotFoundException but it does not have to.

Check this example:

Master a = new test();
a.doFileStuff(); //this line might throw an exception, and you should catch it.
                 //but it is not illegal for test class not to throw an exception

Now, we can do the same thing with a subclass of FileNotFoundException and the same thing with some other exception different than FileNotFoundException. For this later case we will see that our code won't even compile as it is illegal for doFileStuff method in test class to throw a different checked exception that is not FileNotFoundException.

Claudiu
  • 1,469
  • 13
  • 21
0

When overriding a method, you may declare all exceptions, a subset of exceptions, or none of the exceptions thrown by the super class method. You may also declare any exception that is a subclass of an exception declared by the super class method.

You may not throw any exception that is not declared by the super class method, unless it is a subclass of an exception declared by the super class method.

cmd
  • 11,622
  • 7
  • 51
  • 61
0

Not really, just to show in code the reason of Admit's comment

Exceptions can be thrown when you actually have a risk of get them, if you have somewhere in your code throw new FileNotFoundException(); you are forced to add a try catch or throw the exception. However if you are overriding, then you may remove the threat, and if you add some new risks then you have to handle them in the new method.

import java.io.FileNotFoundException;

class Master {
  String doFileStuff() throws FileNotFoundException {
    throw new FileNotFoundException(); // it always throws an exception.
  }
}

public class Test extends Master {

  public static void main(String[] args) {
    String a = new Test().doFileStuff();
    String b = new Master().doFileStuff();
  }

  String doFileStuff() {
    return "a"; //It always returns a. No risks here.
  }

}
porfiriopartida
  • 1,546
  • 9
  • 17
0

It's not necessary for an overriding method to re-declare all the Exceptions thrown by the superclass method. It's only necessary that it doesn't declare Exceptions to be thrown that aren't thrown by the superclass method.

Section 8.4.8.3 of the JLS elaborates:

More precisely, suppose that B is a class or interface, and A is a superclass or superinterface of B, and a method declaration n in B overrides or hides a method declaration m in A. Then:

  • If n has a throws clause that mentions any checked exception types, then m must have a throws clause, or a compile-time error occurs.

  • For every checked exception type listed in the throws clause of n, that same exception class or one of its supertypes must occur in the erasure (§4.6) of the throws clause of m; otherwise, a compile-time error occurs.

  • If the unerased throws clause of m does not contain a supertype of each exception type in the throws clause of n, a compile-time unchecked warning occurs.

That makes sense. Why should a subclass method possibly throw the same Exceptions as the superclass method it overrides? It makes sense for it not to declare additional exceptions, because that would break this scenario:

public class Super
{
   public void method() throws FooException {}
}

public class Sub extends Super {
{
   public void method() throws FooException, BarException {}
}

Then the usage becomes unclear:

Super sup = new Sub();
try {
   sup.method();
}
// But the subclass could (if it were allowed here)
// throw BarException!
catch (FooException e) {}  
rgettman
  • 176,041
  • 30
  • 275
  • 357
0

It's optional in subclass, check below link:

http://www.tutorialspoint.com/java/java_overriding.htm

Meet
  • 242
  • 1
  • 4
  • 13