About Question enthuware.ocpjp.v7.2.1203 :
Moderator: admin
-
- Posts: 31
- Joined: Thu Feb 19, 2015 8:25 am
- Contact:
About Question enthuware.ocpjp.v7.2.1203 :
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?
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?
-
- Site Admin
- Posts: 10115
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1203 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Tue Apr 12, 2016 9:19 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1203 :
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.
m.
-
- Site Admin
- Posts: 10115
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1203 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Tue Apr 12, 2016 9:19 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1203 :
I forgot to import java.io.Closeable. That was the problem. Sorry. Works good now.
m.
m.
-
- Posts: 34
- Joined: Mon Oct 30, 2023 11:55 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1203 :
Does it has the second problem here:
like the previous question? If yes, should it be also mentioned?try(Device d = new Device()){
...a variable declared before the try statement ...
-
- Site Admin
- Posts: 10115
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1203 :
Not sure what you mean.
If you like our products and services, please help us by posting your review here.
-
- Posts: 34
- Joined: Mon Oct 30, 2023 11:55 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1203 :
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.
-
- Site Admin
- Posts: 10115
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1203 :
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.
Not: Device d = new Device(); try(d = new Device()){
So, I am sorry, but I am still not sure what you are getting at.
If you like our products and services, please help us by posting your review here.
-
- Posts: 34
- Joined: Mon Oct 30, 2023 11:55 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1203 :
if you do not see any problem here, this mean I understood some functionality wrong.
Thank you a lot for your help.
Thank you a lot for your help.
Who is online
Users browsing this forum: No registered users and 9 guests