About Question enthuware.ocpjp.v7.2.1203 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Sergiy Romankov
Posts: 31
Joined: Thu Feb 19, 2015 8:25 am
Contact:

About Question enthuware.ocpjp.v7.2.1203 :

Post 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?

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

Re: About Question enthuware.ocpjp.v7.2.1203 :

Post 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.
If you like our products and services, please help us by posting your review here.

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1203 :

Post 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.

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

Re: About Question enthuware.ocpjp.v7.2.1203 :

Post 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.
If you like our products and services, please help us by posting your review here.

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1203 :

Post by Mark7777 »

I forgot to import java.io.Closeable. That was the problem. Sorry. Works good now.

m.

Tester
Posts: 34
Joined: Mon Oct 30, 2023 11:55 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1203 :

Post 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 ...

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

Re: About Question enthuware.ocpjp.v7.2.1203 :

Post by admin »

Not sure what you mean.
If you like our products and services, please help us by posting your review here.

Tester
Posts: 34
Joined: Mon Oct 30, 2023 11:55 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1203 :

Post 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.

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

Re: About Question enthuware.ocpjp.v7.2.1203 :

Post 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.
If you like our products and services, please help us by posting your review here.

Tester
Posts: 34
Joined: Mon Oct 30, 2023 11:55 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1203 :

Post by Tester »

if you do not see any problem here, this mean I understood some functionality wrong.
Thank you a lot for your help.

Post Reply

Who is online

Users browsing this forum: No registered users and 48 guests