About Question enthuware.ocpjp.v7.2.1273 :
Moderator: admin
About Question enthuware.ocpjp.v7.2.1273 :
"If an exception is thrown within the try-with-resources block, then that is the exception that the caller gets"
Should the answer is java.lang.IOException?
Should the answer is java.lang.IOException?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
In the try block inside the main method, new Exception("test") is being thrown. So why do you think IOException should be the right answer?
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocpjp.v7.2.1273 :
So if the code inside try-with-resource throws an exception, the program still runs the code after try-with-resource?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
I am really not sure what you are getting at. The question asks what is the exception thrown out of the main method. So Exception (not IOException) is the right answer. You may also run the code and verify.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocpjp.v7.2.1273 :
Sorry, Here is what i meant:
public static void main(String[] args) throws Exception {
try(Device d = new Device()){ --> an IO exception is thrown here
throw new Exception("test"); --> should this code be run?
}
}
public static void main(String[] args) throws Exception {
try(Device d = new Device()){ --> an IO exception is thrown here
throw new Exception("test"); --> should this code be run?
}
}
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
The given code does not throw any exception in constructor of Device. In fact, the given code does not have any constructor for Device class. So Device d = new Device() doesn't throw any exception.Guest wrote:Sorry, Here is what i meant:
public static void main(String[] args) throws Exception {
try(Device d = new Device()){ --> an IO exception is thrown here
throw new Exception("test"); --> should this code be run?
}
}
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocpjp.v7.2.1273 :
Oh, thanks Paul. I got it now. I should read the question more carefully.
-
- Posts: 17
- Joined: Wed Jan 22, 2014 12:35 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
OP assumed that try-with-resources calls the open() method automatically, which it doesn't.
-
- Posts: 3
- Joined: Sun Sep 28, 2014 11:05 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
Since Device is implementing AutoCloseable, the method close shouldn't declare that it throws an Exception?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
No, why do you think so?
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Sun Sep 28, 2014 11:05 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
Forget that, I was wrong.
About close() method on AutoCloseable:
"While this interface method is declared to throw Exception, implementers are strongly encouraged to declare concrete implementations of the close method to throw more specific exceptions, or to throw no exception at all if the close operation cannot fail."
About close() method on AutoCloseable:
"While this interface method is declared to throw Exception, implementers are strongly encouraged to declare concrete implementations of the close method to throw more specific exceptions, or to throw no exception at all if the close operation cannot fail."
-
- Posts: 14
- Joined: Sun Dec 06, 2015 2:15 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
Can you clarify why AutoCloseable is not imported explicitly -> import java.lang.AutoCloseable ?
I wrote some test code out and if I implement Closeable it does not compile unless I import java.io.Closeable but if I implement AutoCloseable, it compiles without the need to import java.lang.AutoCloseable...
Similarly, AutoCloseable is not imported in this question which lead me to think that it won't compile but apparently that's not a problem...Why?
Thank you
I wrote some test code out and if I implement Closeable it does not compile unless I import java.io.Closeable but if I implement AutoCloseable, it compiles without the need to import java.lang.AutoCloseable...
Similarly, AutoCloseable is not imported in this question which lead me to think that it won't compile but apparently that's not a problem...Why?
Thank you
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
Why do you think any class in java.lang package needs to be imported explicitly? Do you explicitly import java.lang.String? This is OCAJP stuff
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Sun Dec 06, 2015 2:15 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
yeah makes more sense now hahaha....I never actually thought about it
Who is online
Users browsing this forum: No registered users and 40 guests