About Question enthuware.ocajp.i.v7.2.895 :

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

Moderator: admin

Post Reply
fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

About Question enthuware.ocajp.i.v7.2.895 :

Post by fasty23 »

Why doesn't PortConnector constructor need to throws RuntimeException() too as it throw it in it's body?

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.895 :

Post by fasty23 »

fasty23 wrote:Why doesn't PortConnector constructor need to throws RuntimeException() too as it throw it in it's body?
Runtime exceptions do not need to be catch or throws is method signature?

sarakh
Posts: 23
Joined: Fri Jun 20, 2014 3:12 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.895 :

Post by sarakh »

But if we choose to "add throws IOException to the PortConnector constructor. " wouldn't that be wrong?
Because then the method is declaring that it is throwing IOException, and it is also throwing RuntimeException, which is not a subclass of IOException. Why is that valid?

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

Re: About Question enthuware.ocajp.i.v7.2.895 :

Post by admin »

The subclass requirement applies only for checked exceptions. RuntimeExceptions can be thrown even without declaring them in the throws clause.
If you like our products and services, please help us by posting your review here.

Simarpreet Singh
Posts: 7
Joined: Fri Jan 01, 2016 10:56 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.895 :

Post by Simarpreet Singh »

If we declare throws exception only in the PortConnector Constructor and not in main method, why wouldn't that work.

We have provided try catch block in main method, in this case the exception will not be handled successfully, so it will be thrown out to the JVM

Simarpreet Singh
Posts: 7
Joined: Fri Jan 01, 2016 10:56 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.895 :

Post by Simarpreet Singh »

The code is compiling even without declaring throws clause in the main method, please correct the answer

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

Re: About Question enthuware.ocajp.i.v7.2.895 :

Post by admin »

Not sure what you mean. Of course, as per option 5, which is a correct option, it is possible to make it compile without having a throws clause for the main method.
If you like our products and services, please help us by posting your review here.

sahilsaid
Posts: 5
Joined: Tue Oct 11, 2016 4:41 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.895 :

Post by sahilsaid »

Why is it necessary for main method to declare throws IOException (Option 3) when the code is wrapped in try/catch?

If above is true then option 2 should be valid.

Seems like this question can be answered in more than two ways.

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

Re: About Question enthuware.ocajp.i.v7.2.895 :

Post by admin »

Did you read the explanation? It explains exactly what you are asking. The try catch in the main method is only catching RuntimeException. It cannot catch IOException. So if you declare the constructor to throw IOException, the main method has to either catch that exception in the try catch block for IOException or declare IOException in its throws clause.

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

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.895 :

Post by horst1a »

Is there no need to import IOException ?

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

Re: About Question enthuware.ocajp.i.v7.2.895 :

Post by admin »

1. The problem statement clearly includes this line at the top:
//assume appropriate imports

2. Even if you do not see any import statements in the given code, Oracle has specified on its official exam objectives page that you should assume the presence of appropriate import statements.

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

tosidis
Posts: 3
Joined: Fri Oct 26, 2018 4:45 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.895 :

Post by tosidis »

Why do we have to add throws for both IOException and Exception to PortConnector? Just Exception is enough. Even though only IOException is thrown in the PortConnector. So the 1st and the 5th answers could be correct too. Is this a real question from Oracle?

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

Re: About Question enthuware.ocajp.i.v7.2.895 :

Post by admin »

Not sure which option are you refering to because none of the options say that you have to add throws for both IOException and Exception to PortConnector.

Further, notice the phrase 'independent of each other' in the problem statement. Try applying just option 1 and see if the code compiles.
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 29 guests