Page 1 of 1

About Question enthuware.ocpjp.v7.2.1273 :

Posted: Thu Feb 21, 2013 4:48 pm
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?

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

Posted: Thu Feb 21, 2013 6:55 pm
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?

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

Posted: Thu Feb 21, 2013 8:20 pm
by Guest
So if the code inside try-with-resource throws an exception, the program still runs the code after try-with-resource?

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

Posted: Thu Feb 21, 2013 8:26 pm
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.

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

Posted: Thu Feb 21, 2013 8:32 pm
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?
}     
}

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

Posted: Thu Feb 21, 2013 8:36 pm
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.

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

Posted: Thu Feb 21, 2013 8:38 pm
by Guest
Oh, thanks Paul. I got it now. I should read the question more carefully.

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

Posted: Tue Feb 18, 2014 3:07 pm
by EpicWestern
OP assumed that try-with-resources calls the open() method automatically, which it doesn't.

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

Posted: Wed Oct 08, 2014 12:11 am
by jowfornari
Since Device is implementing AutoCloseable, the method close shouldn't declare that it throws an Exception?

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

Posted: Wed Oct 08, 2014 11:29 am
by admin
No, why do you think so?

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

Posted: Wed Oct 08, 2014 4:09 pm
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."

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

Posted: Fri May 20, 2016 3:36 pm
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

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

Posted: Fri May 20, 2016 9:44 pm
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.

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

Posted: Sat May 21, 2016 6:35 am
by codecodecode67
:roll: :lol: yeah makes more sense now hahaha....I never actually thought about it

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

Posted: Sun Sep 29, 2024 9:36 am
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?

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

Posted: Mon Sep 30, 2024 6:56 am
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

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

Posted: Thu Jan 02, 2025 12:30 pm
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?

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

Posted: Thu Jan 02, 2025 11:03 pm
by admin
The book is correct. This statement should be fixed.
thank you for your feedback!