Page 1 of 1

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

Posted: Fri Mar 28, 2014 2:00 pm
by fasty23
Why doesn't PortConnector constructor need to throws RuntimeException() too as it throw it in it's body?

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

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

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

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

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

Posted: Thu Jun 26, 2014 5:47 pm
by admin
The subclass requirement applies only for checked exceptions. RuntimeExceptions can be thrown even without declaring them in the throws clause.

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

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

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

Posted: Thu Jan 14, 2016 10:15 am
by Simarpreet Singh
The code is compiling even without declaring throws clause in the main method, please correct the answer

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

Posted: Thu Jan 14, 2016 10:46 am
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.

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

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

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

Posted: Mon Oct 17, 2016 1:41 am
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.

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

Posted: Mon Jun 12, 2017 2:19 am
by horst1a
Is there no need to import IOException ?

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

Posted: Mon Jun 12, 2017 3:41 am
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.

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

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

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

Posted: Sat Nov 17, 2018 11:22 am
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.