Page 1 of 1

Possible incorrect answer in exam bank

Posted: Thu Nov 26, 2020 3:34 pm
by angersleek
This is about question 52 in Foundation Test 3 no: 52

Q: Which of the following statements are true?

The following is NOT correct based on the test.
An overriding method can declare that it throws a wider spectrum of checked exceptions than the method it is overriding.

But isn't this correct?

The following example showcases this. Where the overriding method is in fact throwing a wider spectrum of checked exceptions and compiles fine.
Am I missing something, misread the question / answer? Please advice. Thank you.

Code: Select all

import java.io.IOException;

public class App extends Haha {

    @Override
    void test() throws IOException, InterruptedException, NoSuchMethodException, IllegalAccessException {

    }
}

class Haha {

    void test() throws Exception {

    }
}

Re: Possible incorrect answer in exam bank

Posted: Thu Nov 26, 2020 8:27 pm
by admin
IOException and InterruptedException are subclasses of Exception. So, it is not wider but narrower list of exceptions.
Wider means super class, narrower means sub class.
This is explained nicely in section 11.4.1 Overriding methods of Hanumant Deshmukh's OCAJP 8 Fundamentals book.