About Question enthuware.ocpjp.v7.2.1240 :
Moderators: Site Manager, fjwalraven
-
- Posts: 132
- Joined: Thu May 16, 2013 9:23 am
- Contact:
About Question enthuware.ocpjp.v7.2.1240 :
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.
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.
-
- Posts: 132
- Joined: Thu May 16, 2013 9:23 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
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.
The_Nick.
-
- Posts: 17
- Joined: Wed Nov 26, 2014 1:59 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
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
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
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
Why do you think the correct option is incorrect? Your output is exactly what is claimed by the option.
-
- Posts: 31
- Joined: Tue Oct 06, 2015 1:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
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).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
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.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
Oh ok. Thank you for your feedback!
Paul.
Paul.
-
- Posts: 5
- Joined: Mon Nov 23, 2015 9:37 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
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);
}
}
}
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);
}
}
}
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
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.
-
- Posts: 5
- Joined: Mon Nov 23, 2015 9:37 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
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?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
No, put your code within .
Code: Select all
and
-
- Posts: 5
- Joined: Mon Nov 23, 2015 9:37 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
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);
}}}
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
Why do you think close method should be called and when?
-
- Posts: 5
- Joined: Mon Nov 23, 2015 9:37 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
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?
"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?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
That is correct.
-
- Posts: 5
- Joined: Mon Nov 23, 2015 9:37 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1240 :
Thank you so much.
Who is online
Users browsing this forum: No registered users and 8 guests