Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1093 :

Posted: Tue Dec 08, 2015 12:55 pm
by dannysantos1985
1)If the method1() of Base did throw a IOException with a "throw new IOException();", does the method1() of NewBase needs a throws IOException?
2)If someMethod() did throw a IOException, then the NewBase method1() would need a throws IOException, right?

Re: About Question enthuware.ocajp.i.v8.2.1093 :

Posted: Tue Dec 08, 2015 8:06 pm
by admin
What happened when you tried it out?
Remember that what exception a method actually throws at runtime is immaterial for the compiler. Compiler can only check at compile time what a method can possibly throw at rutime.
Paul.

Re: About Question enthuware.ocajp.i.v8.2.1093 :

Posted: Wed Dec 09, 2015 10:55 am
by dannysantos1985
1) No, it doesn't.
2) Yes, if the IOException it isn't caught in someMethod() or in method1() of NewBase.

Re: About Question enthuware.ocajp.i.v8.2.1093 :

Posted: Wed Dec 09, 2015 11:02 am
by admin
Great!

Re: About Question enthuware.ocajp.i.v8.2.1093 :

Posted: Wed Dec 09, 2015 11:18 am
by dannysantos1985
I understand that the overridding method needs to specify a subset of the exceptions of the overridden method, and I used to think that the overridding method could not add new checked exceptions without mentioning them in the signature of the overridden method. Is that wrong what I just said?
I believe that I read it from a book. Did it used to be true? Because I just tried that in my IDE and everything that I used to know didn't happen, it compiled perfectly without any errors, and it shouldn't have let me put a new checked Exception in the overridding method...

Re: About Question enthuware.ocajp.i.v8.2.1093 :

Posted: Wed Dec 09, 2015 8:55 pm
by admin
dannysantos1985 wrote:I understand that the overridding method needs to specify a subset of the exceptions of the overridden method, and I used to think that the overridding method could not add new checked exceptions without mentioning them in the signature of the overridden method. Is that wrong what I just said?
It is correct that an overriding method cannot throw any new checked exception. But "new" means an exception that is not a subclass of the existing exception. For example, if the base method throws IOException, the subclass method can throw FileNotFoundException because FileNotFoundException is not really new in the sense that it is already covered in IOException.

To understand this, you have to think from the perspective of the user of a class:
Base b = new Derived();
b.method1();
here the user expects that method1 can throw IOException. So he is prepared for that. Even if the actual class of the object i.e. Derived, throws FNE, he is not worried because he is already prepared for IOException (i.e. using a try/catch or throws clause of its own). But if Derived throws some other exception that is not in the hierarchy such as SQLException, then the caller is not prepared for that and his code will break.

This is important because a user of Base class should be able to use any object that is-a Base without any trouble.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v8.2.1093 :

Posted: Thu Dec 10, 2015 9:20 am
by dannysantos1985
I tried exactly with FileNotFoundException in a IOException and it was strange because it worked. I didn't know that FileNotFoundException is a subclass of IOException! Thank you a lot. You are great

Re: About Question enthuware.ocajp.i.v8.2.1093 :

Posted: Fri Feb 10, 2017 3:59 pm
by jamesmccreary
In response to the explanation:
Overriding method only needs to specify a subset of the list of exception classes the overridden method can throw. A set of no classes is a valid subset of that list.
Is specifying a set of no classes (i.e. no "throws" clause) a subset of every exception? This seems a bit disjointed, given that a set of no classes exception does not fit into the exception hierarchy, thus I do not see how it can be a subset. Even if this is the case, at what point is this set of no classes exception not a subset of an exception? I checked the JLS and could not find anything on the matter.

Re: About Question enthuware.ocajp.i.v8.2.1093 :

Posted: Fri Feb 10, 2017 9:37 pm
by admin
Yes, specifying a set of no classes (i.e. no "throws" clause) a subset of every exception. An empty set is a subset of every set : http://math.stackexchange.com/questions ... -every-set

Hope you are not confusing subset with sub class.

It has nothing to do with JLS. Please see my response above posted on Thu Dec 10, 2015 1:55 am. It explains the logic behind the throws clause of an overriding method.