Page 1 of 1

About Question enthuware.ocpjp.v7.2.1145 :

Posted: Sun Feb 01, 2015 5:46 pm
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)

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

Posted: Sun Feb 01, 2015 9:50 pm
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.

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

Posted: Sun Feb 01, 2015 11:10 pm
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.

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

Posted: Mon Feb 02, 2015 3:22 am
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.

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

Posted: Mon Feb 02, 2015 10:38 pm
by gupta.v21
you are absolutely right which i could not capture .

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

Posted: Wed Jan 18, 2017 3:31 am
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! :(

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

Posted: Wed Jan 18, 2017 10:07 am
by admin
Did you read the explanation? It tells you exactly why this question is there.

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

Posted: Mon Jul 24, 2017 10:57 am
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.

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

Posted: Fri Sep 08, 2017 2:18 am
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

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

Posted: Fri Sep 08, 2017 3:31 am
by admin
The exception class used in the code is NoSuchFieldException , not NoSuchFileException.

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

Posted: Sun Apr 04, 2021 6:59 am
by syzygy
the explaination should include something about rethrown exceptions keeping their runtime type

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

Posted: Mon Apr 05, 2021 1:01 am
by admin
Added.
thank you for your feedback!

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

Posted: Sun Dec 24, 2023 6:47 am
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.

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

Posted: Sun Dec 24, 2023 8:44 pm
by admin
Fair point.
thank you for your feedback!