About Question enthuware.ocpjp.v7.2.1145 :

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

Moderator: admin

Post Reply
gupta.v21
Posts: 17
Joined: Wed Jan 07, 2015 11:33 pm
Contact:

About Question enthuware.ocpjp.v7.2.1145 :

Post by gupta.v21 »

As per the explanation if ClassNotFoundException and NoSuchFieldException are checked Exceptions
then how catch (RuntimeException e) is valid catch clause.
should not it be catch(Exception e)

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

Re: About Question enthuware.ocpjp.v7.2.1145 :

Post by admin »

Why do you think it is not valid?
Checked exceptions can either be caught in a catch block or they can be declared in the throws clause of the method.
If you like our products and services, please help us by posting your review here.

gupta.v21
Posts: 17
Joined: Wed Jan 07, 2015 11:33 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1145 :

Post by gupta.v21 »

How can you catch Checked Exception with RuntimeException Since RuntimeException is a subclass of Exception.

if you are saying try block is going to throw some checked exception is it valid to catch it with runtime exception.
i tried same code in eclipse and code says unhandled Exception.


And infact if (RuntimeException e) is valid then

no throws Clause is necessary is also a valid option.

Please refer to screen shot attached.
Attachments
Error.JPG
Error.JPG (49.86 KiB) Viewed 9424 times

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

Re: About Question enthuware.ocpjp.v7.2.1145 :

Post by admin »

I did not say that you can catch a checked exception with a catch clause for RuntimeException. I said the catch clause in the given question is valid. As I said, checked exceptions can either be caught in a catch block or they can be declared in the throws clause of the method. That is what the question is getting at. The checked exceptions are not being caught by the catch clause present in the code. That is why the question is asking about the throws clause of the method.
Remember that a catch clause doesn't necessarily have to catch all the exceptions. It can be selective. That doesn't make it invalid.

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

gupta.v21
Posts: 17
Joined: Wed Jan 07, 2015 11:33 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1145 :

Post by gupta.v21 »

you are absolutely right which i could not capture .

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1145 :

Post by jagoneye »

What? This is insane? They haven't mentioned reflection in the syllabus!! This is totally unfair if they ask about in the exam! Reflection is very advanced and complex concept and it is only meant for system or library designers not for application developers! :(

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

Re: About Question enthuware.ocpjp.v7.2.1145 :

Post by admin »

Did you read the explanation? It tells you exactly why this question is there.
If you like our products and services, please help us by posting your review here.

Cannelids
Posts: 25
Joined: Thu Jun 01, 2017 2:50 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1145 :

Post by Cannelids »

This tripped me up as I didn’t know whether ClassNotFoundException and/or NoSuchFieldException were checked. However on reflection in the less stressful after-test state :p, I realised that both being checked is the only combination that gives exactly two correct options.

kakawi
Posts: 5
Joined: Sat Aug 12, 2017 1:43 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1145 :

Post by kakawi »

Hi

If I understand correctly, now exception NoSuchFileException - doesn't have constructor without any parameters. Only

Code: Select all

public NoSuchFileException(String file) {...}
public NoSuchFileException(String file, String other, String reason){...}
So the code is not compile at all

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

Re: About Question enthuware.ocpjp.v7.2.1145 :

Post by admin »

The exception class used in the code is NoSuchFieldException , not NoSuchFileException.
If you like our products and services, please help us by posting your review here.

syzygy
Posts: 3
Joined: Wed Mar 31, 2021 2:56 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1145 :

Post by syzygy »

the explaination should include something about rethrown exceptions keeping their runtime type

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

Re: About Question enthuware.ocpjp.v7.2.1145 :

Post by admin »

Added.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

andrew1584
Posts: 8
Joined: Sat Oct 28, 2023 9:27 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1145 :

Post by andrew1584 »

I think it would be better to mention that ClassNotFoundException and NoSuchFieldException are checked exceptions at the question, because these exceptions are not a part of objectives.

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

Re: About Question enthuware.ocpjp.v7.2.1145 :

Post by admin »

Fair point.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 39 guests