About Question enthuware.ocpjp.v7.2.1240 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

About Question enthuware.ocpjp.v7.2.1240 :

Post by The_Nick »

Hi,
I have tried to change implements AutoClosable in only Closable;
and it does not compile
I thought that in java 7 closable extends AutoClosable isn't it?

Thanks in advance.

The_Nick.

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

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

Post by The_Nick »

I add a little thing that might be useful: if NOT used the try statement with resource, instead it's used the classic try catch-finally block the exception coming from finally will be shown whereas the ones in the try block will be suppressed. The other way round just to complicate things. Very good indeed...

The_Nick.

leorbarbosa
Posts: 17
Joined: Wed Nov 26, 2014 1:59 pm
Contact:

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

Post by leorbarbosa »

Hi all,

there is a problem in this question. It is a good logical test, but the answers shown in the option list are not correct.

My output was:
D1 Opened
Closing device D2
Exception in thread "main" java.io.IOException: Unknown
at trywithresources.Device.<init>(Device.java:10)
at trywithresources.Device.main(Device.java:16)
Suppressed: java.lang.RuntimeException: RTE while closing D2
at trywithresources.Device.close(Device.java:28)
at trywithresources.Device.main(Device.java:18)
Java Result: 1

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

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

Post by admin »

Why do you think the correct option is incorrect? Your output is exactly what is claimed by the option.

krohani
Posts: 31
Joined: Tue Oct 06, 2015 1:57 pm
Contact:

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

Post by krohani »

leorbarbosa wrote:Hi all,

there is a problem in this question. It is a good logical test, but the answers shown in the option list are not correct.

My output was:
D1 Opened
Closing device D2
Exception in thread "main" java.io.IOException: Unknown
at trywithresources.Device.<init>(Device.java:10)
at trywithresources.Device.main(Device.java:16)
Suppressed: java.lang.RuntimeException: RTE while closing D2
at trywithresources.Device.close(Device.java:28)
at trywithresources.Device.main(Device.java:18)
Java Result: 1
Admin - the previous poster's results are "Closing device D2" whereas in the question bank the answer suggested says "Closing device D1". For the previous poster the second resource actually gets created and the system is attempting to close that one first before the first resource (which is correct).

Previous Poster - I tested it and received the same answer as what the software says it should be "Closing device D1". D2 never gets created and therefore should never be called to close.

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

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

Post by admin »

Oh ok. Thank you for your feedback!
Paul.

Simen Sanjeetha
Posts: 5
Joined: Mon Nov 23, 2015 9:37 am
Contact:

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

Post by Simen Sanjeetha »

Hi,

I tried the code for this question in eclipse and ran it and got the same reply as the software. But when i try a similar code (given below), the close method does not execute. Can you please clarify?

import java.io.IOException;
class Device implements AutoCloseable{
public Device() throws IOException{
System.out.println("open");
throw new IOException();
}
public void close() {
System.out.println("close");
throw new RuntimeException();
}
}
public class Testclass{

public static void main(String a[]) throws Exception {
try(Device d = new Device()){
System.out.println("try!");
throw new Exception();

}
catch(Exception e){
System.out.println("catch");
System.out.println(e);
for (Throwable t : e.getSuppressed())
System.out.println(t);

}
}
}

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

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

Post by admin »

The code is hard to read without indwntation. Please Wrap it within

Code: Select all

 tag and indicate which line did you expect to execute but did not execute.

Simen Sanjeetha
Posts: 5
Joined: Mon Nov 23, 2015 9:37 am
Contact:

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

Post by Simen Sanjeetha »

Oh sorry! This is my first time so i dont know how to post code. I didnt understand what you mean by "wrap it within

Code: Select all

 tag". Should i put whole code within square brackets?

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

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

Post by admin »

No, put your code within

Code: Select all

 and 
.

Simen Sanjeetha
Posts: 5
Joined: Mon Nov 23, 2015 9:37 am
Contact:

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

Post by Simen Sanjeetha »

Why does the close method not execute ?

Code: Select all

import java.io.IOException;
class Device implements AutoCloseable{
public Device() throws IOException{
System.out.println("open");
throw new IOException();
}
public void close() {
System.out.println("close");
throw new RuntimeException();
}
}
public class Testclass{

public static void main(String a[]) throws Exception {
try(Device d = new Device()){
System.out.println("try!");
throw new Exception();

}
catch(Exception e){
System.out.println("catch");
System.out.println(e);
for (Throwable t : e.getSuppressed())
System.out.println(t);

}}}

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

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

Post by admin »

Why do you think close method should be called and when?

Simen Sanjeetha
Posts: 5
Joined: Mon Nov 23, 2015 9:37 am
Contact:

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

Post by Simen Sanjeetha »

In the explanation it says,

"Any exception that is thrown while closing a resource is added to the list of suppressed exceptions of the exception thrown while opening a resource(or thrown from the try block)."

So i got confused and expected the close method to execute while opening Device.

I got it now, in question 1240, there are two resources being opened and the close method executes only for the resource that successfully opens without throwing an exception. Right?

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

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

Post by admin »

That is correct.

Simen Sanjeetha
Posts: 5
Joined: Mon Nov 23, 2015 9:37 am
Contact:

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

Post by Simen Sanjeetha »

Thank you so much.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests