About Question enthuware.ocpjp.v7.2.1273 :

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

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocpjp.v7.2.1273 :

Post by ETS User »

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

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

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

Post by admin »

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.

Guest

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

Post by Guest »

So if the code inside try-with-resource throws an exception, the program still runs the code after try-with-resource?

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

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

Post by admin »

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

Guest

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

Post by Guest »

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

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

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

Post by admin »

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?
}     
}
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.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Guest

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

Post by Guest »

Oh, thanks Paul. I got it now. I should read the question more carefully.

EpicWestern
Posts: 17
Joined: Wed Jan 22, 2014 12:35 pm
Contact:

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

Post by EpicWestern »

OP assumed that try-with-resources calls the open() method automatically, which it doesn't.

jowfornari
Posts: 3
Joined: Sun Sep 28, 2014 11:05 am
Contact:

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

Post by jowfornari »

Since Device is implementing AutoCloseable, the method close shouldn't declare that it throws an Exception?

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

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

Post by admin »

No, why do you think so?
If you like our products and services, please help us by posting your review here.

jowfornari
Posts: 3
Joined: Sun Sep 28, 2014 11:05 am
Contact:

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

Post by jowfornari »

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

codecodecode67
Posts: 14
Joined: Sun Dec 06, 2015 2:15 pm
Contact:

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

Post by codecodecode67 »

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

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

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

Post by admin »

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

codecodecode67
Posts: 14
Joined: Sun Dec 06, 2015 2:15 pm
Contact:

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

Post by codecodecode67 »

:roll: :lol: yeah makes more sense now hahaha....I never actually thought about it

Post Reply

Who is online

Users browsing this forum: No registered users and 48 guests