1

I'm trying to understand the difference why a child class cannot override a method implementation in the parent class to catch an Exception but no problem while catching a Error,

For example in the below scenario, when I remove "Exception" from the throws clause, it compiles fine.

class Supertest {

    public void amethod(int i, String s) {

    }

}

public class test extends Supertest {

    public void amethod(int i, String s) throws Error, Exception {

    }

}
user1050619
  • 19,822
  • 85
  • 237
  • 413
  • Possible Duplicate: http://stackoverflow.com/questions/5875414/method-overriding-and-exceptions – dcp Sep 18 '13 at 17:28

4 Answers4

8

Error is an unchecked exception. Exception is a checked exception. It's as simple as that. So it should be reasonable to have code like this:

Supertest x = new test();
x.amethod(10, "foo");

... but test.amethod() tries to impose a checked exception on the caller, so that the caller either has to catch it or propagate it. As the method it's overriding doesn't declare that exception, the overriding method can't either.

As noted in comments, fundamentally you can't override one method with a "more restrictive" one - any call to the original method must still be valid to the override.

From section 8.4.8.3 of the JLS:

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.
Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    @OP throws clause apart, in general you cannot impose greater restriction on the overridden method. This includes changing return type from say `Integer` to `Number`. – Rohit Jain Sep 18 '13 at 17:33
  • Indeed - or change a method parameter from `Number` to `Integer`. – Jon Skeet Sep 18 '13 at 17:35
  • I guess changing parameter type will be compilable, and considered as Method overloading. – Rohit Jain Sep 18 '13 at 17:38
  • @RohitJain: It depends on whether you use `@Override` :) (But yes, they're fundamentally different to return types in that they're part of the signature. My bad. I'd thought Java had contravariant parameters in some places, but that's just my poor memory.) – Jon Skeet Sep 18 '13 at 17:39
2

Throw clauses must be covariant.

This is similar to the requirement of return types of overriding methods:

SuperClass
    Pet method()

SubClass
    Cat method()

here Cat is a subtype of Pet, therefore the overriding is legal.

The same principle applies to throws clause, which is also part of the output of the method.

SuperClass
    Pet method() throws PetException

SubClass
    Cat method() throws CatException

this is legal if CatException is a subtype of PetException.

If the throw clause of a method is empty, it is implicitly RuntimeException|Error. All overriding methods must only throw subtypes of that. Excepiton|RuntimeException|Error is not a subtype of RuntimeException|Error.

ZhongYu
  • 19,446
  • 5
  • 33
  • 61
0

Say you have

SuperTest superTest = new test();
superTest.amethod();

at compile time, methods are resolved with the declared or static type of the variable. The SuperTest#amethod is declared without throwing the checked exception Exception so its children need to follow those rules.

Error is an unchecked exception. It doesn't need to be caught. As such the parent method declaration doesn't restrict it.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

If super class method doesn't declare any exception then subclass overridden method cannot declare any checked exception.

Here is Exception is checked and Error is unchecked.So you can't throw Exception.
Reference http://www.javatpoint.com/exception-handling-with-method-overriding

Prabhaker A
  • 8,317
  • 1
  • 18
  • 24