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

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 » Fri Mar 28, 2014 2:00 pm

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 » Fri Mar 28, 2014 2:16 pm

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 » Thu Jun 26, 2014 3:37 pm

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: 8516
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Thu Jun 26, 2014 5:47 pm

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 » Thu Jan 14, 2016 10:09 am

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 » Thu Jan 14, 2016 10:15 am

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

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

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

Post by admin » Thu Jan 14, 2016 10:46 am

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 » Sun Oct 16, 2016 3:24 pm

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: 8516
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Mon Oct 17, 2016 1:41 am

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 » Mon Jun 12, 2017 2:19 am

Is there no need to import IOException ?

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

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

Post by admin » Mon Jun 12, 2017 3:41 am

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 » Sat Nov 17, 2018 10:58 am

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: 8516
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat Nov 17, 2018 11:22 am

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: No registered users and 2 guests