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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
dannysantos1985
Posts: 12
Joined: Tue Nov 24, 2015 4:34 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

dannysantos1985
Posts: 12
Joined: Tue Nov 24, 2015 4:34 pm
Contact:

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

Post by dannysantos1985 »

1) No, it doesn't.
2) Yes, if the IOException it isn't caught in someMethod() or in method1() of NewBase.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Great!
If you like our products and services, please help us by posting your review here.

dannysantos1985
Posts: 12
Joined: Tue Nov 24, 2015 4:34 pm
Contact:

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

Post 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...

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

dannysantos1985
Posts: 12
Joined: Tue Nov 24, 2015 4:34 pm
Contact:

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

Post 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

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests