About Question enthuware.ocpjp.v7.2.1273 :

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
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: 10383
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?

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: 10383
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.

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: 10383
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.

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: 10383
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?

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: 10383
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.

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

_umut_
Posts: 6
Joined: Sun Sep 15, 2024 12:42 am
Contact:

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

Post by _umut_ »

Hi,
First of all, thank you for providing such an excellent resource for OCP exam preparation! I’ve found your tests and explanations incredibly helpful.

I noticed that the explanation regarding exceptions in try-with-resources might be a bit unclear for less experienced developers, like myself. It focuses on how exceptions from the close() method are suppressed, but it doesn’t emphasize the important difference when an explicit finally block is involved.

For example, in this case:
try (Device d = new Device()) {
throw new Exception("test");
}
finally {
throw new RuntimeException();
}

The exception from the finally block actually overrides both the exception thrown in the try block and any exceptions from close():
Exception in thread "main" java.lang.RuntimeException
at ocp.exception.Device.main(Device.java:30)

Also, when the explanation refers to the “try-with-resources block,” it might be useful to clarify that it includes the catch and finally blocks as well. This distinction is crucial and could lead to misunderstandings if not highlighted. I personally learned it wrong at first due to the current explanation.

Maybe consider updating the explanation to make this clearer?

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

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

Post by admin »

Good point. Will make this point clear in the explanation.

thank you for your feedback!

P.S. This point is explained in detail in https://amzn.to/3JHjZQv

szylica
Posts: 4
Joined: Thu Jan 02, 2025 12:25 pm
Contact:

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

Post by szylica »

if a try-with-resources block has a finally block and if an exception is thrown from that finally block, then the exception thrown from the finally block is received by the caller and the exception thrown from the try-with-resources block and the exceptions thrown while closing resources are added as suppressed exception to the exception thrown from the finally block.
in OCP Study Guide by Scott Selikoff and Jeanne Boyarsky they said that exceptions thrown in finally block, while closing resources are lost, not added as suppressed exception.

I tested it by adding a finally block with another exception and there's nothing about the previous exceptions

So what is true? They are added to suppressed or lost?

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

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

Post by admin »

The book is correct. This statement should be fixed.
thank you for your feedback!

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests