Page 1 of 1
About Question enthuware.ocpjp.v7.2.1240 :
Posted: Sun Jun 16, 2013 11:30 am
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.
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Sun Jun 16, 2013 11:42 am
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.
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Mon Jun 22, 2015 1:30 pm
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
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Mon Jun 22, 2015 8:15 pm
by admin
Why do you think the correct option is incorrect? Your output is exactly what is claimed by the option.
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Tue Oct 13, 2015 11:45 pm
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.
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Wed Oct 14, 2015 9:29 pm
by admin
Oh ok. Thank you for your feedback!
Paul.
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Mon Nov 23, 2015 9:53 am
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);
}
}
}
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Mon Nov 23, 2015 10:59 am
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.
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Wed Nov 25, 2015 2:33 am
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?
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Wed Nov 25, 2015 9:39 am
by admin
No, put your code within
.
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Fri Nov 27, 2015 10:52 am
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);
}}}
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Fri Nov 27, 2015 11:04 am
by admin
Why do you think close method should be called and when?
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Fri Nov 27, 2015 11:17 am
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?
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Fri Nov 27, 2015 6:53 pm
by admin
That is correct.
Re: About Question enthuware.ocpjp.v7.2.1240 :
Posted: Fri Nov 27, 2015 8:26 pm
by Simen Sanjeetha
Thank you so much.