0

Looking for verification on the following:

In the implemented/overridden method, Java concerns itself ONLY with the checked exceptions declared to be thrown.

Eg.: To the interface:

interface INT { void mm() throws IOException, ArrayIndexOutOfBoundsException; }

,

class Some implements INT {
    void mm() {...}
    // ...
}

doesn't give a checked error. Neither does the following (since NullPointerException is a RunimeException):

class Some implements INT {
    void mm() throws NullPointerException {...}
    // ...
}

while

class Some implements INT {
    void mm() throws ActivationException {...}
    // ...
}

gives a compiler error since ActivationException is a checked exception and its super-class implementation isn't throwing it (or a sub-class of it).

Note here:

class Some implements INT {
    void mm() throws IndexOutOfBoundsException {...}
    // ...
}

doesn't give a compile-time error although IndexOutOfBoundsException is super to ArrayIndexOutOfBoundsException since they are runtime exceptions.

Also note:

class Some implements INT {
    void mm() throws Exception {...}
    // ...
}

gives a checked error since Exception can also be a checked exception and is super to those in the overridden one.

In the overall,

an exception declared to be thrown in the overriding method gets a compiler error ONLY IF it is a checked exception and it isn't (or a super-type of it isn't) thrown in the overridden method of its super class.

This is exception handling in the implemented method more elaborate and extensive. Asking it again-- couldn't get an answer there to its narrower version.

TIA.

Community
  • 1
  • 1
user3880721
  • 613
  • 6
  • 16
  • "*In the implemented/overridden method, Java concerns itself ONLY with the checked exceptions declared to be thrown*" yes that is correct, you can't add new type of checked exceptions to overriding method unless they are derived from already declared checked exceptions declared in in overridden method. It is because `Base b = ....` can hold any subtype of `Base` class, so compiler can't assume instance of which subtype will be used here, and which additional exceptions will need to be handled, which means that some of them could not be caught (which would be incredible security problem). – Pshemo Sep 06 '14 at 22:00
  • This problem doesn't exist in case of declaring exception of type derived from already declared ones because derived exceptions can be caught by `catch` blocks handling their supertypes. Also you can add any RuntimeException because they don't need to be handled. – Pshemo Sep 06 '14 at 22:02
  • @Pshemo write these as an ans n i'll accept. pls also verify that you verify everything in the Q-- for clarity. – user3880721 Sep 06 '14 at 22:06
  • So basically the answer you ware looking for was "yes, that is correct" :). In that case feel free to delete this question since you already knew the answer and I have a feeling that SO already has few similar questions with even better answers so mine is not needed that much. – Pshemo Sep 06 '14 at 22:09
  • @Pshemo wont delete-- no. these r my look-up notes for latter. laid it out all fine. so be it if you don't wanna go for it. points don't count much w/me. – user3880721 Sep 06 '14 at 22:12
  • @Pshemo where are thos "Qs with better answers"? couldn't some across myself. – user3880721 Sep 06 '14 at 22:13
  • Just an advice, don't use SO as main place for your notes, to many things depend on others. Create your own blog or set of documents and write it down there. You can think of SO as backup notes if you wish, it is OK to ask question and post answer to it if question is not very basic (basic ones tend to attract downvotes and being deleted). – Pshemo Sep 06 '14 at 22:21
  • To find answers to your question you can simply use this query [`[java] new exception in overridden method`](http://stackoverflow.com/search?q=%5Bjava%5D+new+exception+in+overridden+method) and you will find for instance my [previous answer](http://stackoverflow.com/a/10913075/1393766) on this subject which is similar to my previous comments. Below this question there was a comment pointing to [even better answer](http://stackoverflow.com/a/5875455/1393766). – Pshemo Sep 06 '14 at 22:22

0 Answers0