user1050619
user1050619

Reputation: 20886

Throw Error, Exception and Runtime Exception in child class

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 {

    }

}

Upvotes: 1

Views: 2603

Answers (4)

ZhongYu
ZhongYu

Reputation: 19682

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.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1502186

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.

Upvotes: 8

Prabhaker A
Prabhaker A

Reputation: 8473

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

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280102

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.

Upvotes: 0

Related Questions