About Question enthuware.ocpjp.v7.2.1145 :
Moderator: admin
-
- Posts: 17
- Joined: Wed Jan 07, 2015 11:33 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1145 :
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)
then how catch (RuntimeException e) is valid catch clause.
should not it be catch(Exception e)
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1145 :
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.
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.
-
- Posts: 17
- Joined: Wed Jan 07, 2015 11:33 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1145 :
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.
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 (49.86 KiB) Viewed 9776 times
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1145 :
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.
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.
-
- Posts: 17
- Joined: Wed Jan 07, 2015 11:33 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1145 :
you are absolutely right which i could not capture .
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1145 :
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!
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1145 :
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.
-
- Posts: 25
- Joined: Thu Jun 01, 2017 2:50 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1145 :
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.
-
- Posts: 5
- Joined: Sat Aug 12, 2017 1:43 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1145 :
Hi
If I understand correctly, now exception NoSuchFileException - doesn't have constructor without any parameters. Only
So the code is not compile at all
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){...}
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1145 :
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.
-
- Posts: 3
- Joined: Wed Mar 31, 2021 2:56 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1145 :
the explaination should include something about rethrown exceptions keeping their runtime type
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1145 :
Added.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Sat Oct 28, 2023 9:27 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1145 :
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.
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1145 :
Fair point.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 28 guests