Page 1 of 1
About Question enthuware.ocpjp.v7.2.1203 :
Posted: Wed Mar 30, 2016 5:03 am
by Sergiy Romankov
I wonder, why this method is compiled without problem
public String read()throws IOException{
throw new IOException("Unknown");
}
It must return String value, but instead throws Exception.
Futhermore I can not put return "" neither before throw excpression
nor after in both cases i get compilation error "Unreachable code".
Could you please explain, how it works here?
Re: About Question enthuware.ocpjp.v7.2.1203 :
Posted: Wed Mar 30, 2016 12:07 pm
by admin
1. You need to understand that the compiler doesn't execute any code. It just goes through the code and draws as many broad inferences from it as possible. The inferences that it draws basically depends on how the language designers designed the language and, to some extent, on the limitations of lexical analysis. There is no exact logical reason for all such inferences except that "because the specification says so"
Here, the compiler doesn't know that the method will always throw an exception (although you do). From the compiler's perspective, all the statements inside the method are valid and so the method is valid.
Next, while going through the method code, it knows that nothing after the statement "throw new IOException("Unknown");" can execute. So it prevents you from writing anything after it. Same logic prevents you from writing a return statement before the throw statement.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1203 :
Posted: Sun Nov 04, 2018 6:35 pm
by Mark7777
I don't understand why it won't compile when I use implements Closeable. It works when I implement AutoCloseable. Any ideas? Is it simply because the rules say it must? If so, when would Closeable work? I know a Closeable can only throw an IOException or RuntimeException. I would think Closeable would do the trick.
m.
Re: About Question enthuware.ocpjp.v7.2.1203 :
Posted: Sun Nov 04, 2018 9:07 pm
by admin
What exactly doesn't compile? What error message do you get when you compile? java.io.Closeable implements java.lang.AutoCloseable, so, anything that requires AutoCloseable will work with Closeable.
Re: About Question enthuware.ocpjp.v7.2.1203 :
Posted: Sun Nov 04, 2018 9:46 pm
by Mark7777
I forgot to import java.io.Closeable. That was the problem. Sorry. Works good now.
m.
Re: About Question enthuware.ocpjp.v7.2.1203 :
Posted: Mon Nov 06, 2023 1:44 pm
by Tester
Does it has the second problem here:
try(Device d = new Device()){
like the previous question? If yes, should it be also mentioned?
...a variable declared before the try statement ...
Re: About Question enthuware.ocpjp.v7.2.1203 :
Posted: Mon Nov 06, 2023 6:08 pm
by admin
Not sure what you mean.
Re: About Question enthuware.ocpjp.v7.2.1203 :
Posted: Tue Nov 07, 2023 1:56 am
by Tester
if I understand right it has one more mistake like:
The try-with-resource was enhanced in Java 9 and it now allows you to use a variable declared before the try statement in the try-with-resource block as long as it is final or effectively final. For example, the following is valid since Java 9: Statement stmt = c.createStatement(); try(stmt){ ... } However, try(stmt = c.createStatement();) is still invalid.
Re: About Question enthuware.ocpjp.v7.2.1203 :
Posted: Tue Nov 07, 2023 3:20 am
by admin
But the code given in the problem statement has: try(Device d = new Device()){
Not: Device d = new Device(); try(d = new Device()){
So, I am sorry, but I am still not sure what you are getting at.
Re: About Question enthuware.ocpjp.v7.2.1203 :
Posted: Tue Nov 07, 2023 5:00 am
by Tester
if you do not see any problem here, this mean I understood some functionality wrong.
Thank you a lot for your help.