About Question enthuware.ocajp.i.v7.2.895 :
Moderator: admin
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
About Question enthuware.ocajp.i.v7.2.895 :
Why doesn't PortConnector constructor need to throws RuntimeException() too as it throw it in it's body?
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.895 :
Runtime exceptions do not need to be catch or throws is method signature?fasty23 wrote:Why doesn't PortConnector constructor need to throws RuntimeException() too as it throw it in it's body?
-
- Posts: 23
- Joined: Fri Jun 20, 2014 3:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.895 :
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?
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?
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.895 :
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.
-
- Posts: 7
- Joined: Fri Jan 01, 2016 10:56 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.895 :
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
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
-
- Posts: 7
- Joined: Fri Jan 01, 2016 10:56 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.895 :
The code is compiling even without declaring throws clause in the main method, please correct the answer
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.895 :
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.
-
- Posts: 5
- Joined: Tue Oct 11, 2016 4:41 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.895 :
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.
If above is true then option 2 should be valid.
Seems like this question can be answered in more than two ways.
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.895 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 37
- Joined: Mon Jun 12, 2017 2:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.895 :
Is there no need to import IOException ?
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.895 :
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.
//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.
-
- Posts: 3
- Joined: Fri Oct 26, 2018 4:45 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.895 :
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?
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.895 :
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.
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.
Who is online
Users browsing this forum: No registered users and 49 guests