About Question enthuware.ocpjp.v7.2.1273 :
Moderators: Site Manager, fjwalraven
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: 10384
- 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?
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: 10384
- 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.
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: 10384
- 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.
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: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
No, why do you think so?
-
- 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: 10384
- 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.
-
- Posts: 14
- Joined: Sun Dec 06, 2015 2:15 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :


-
- Posts: 6
- Joined: Sun Sep 15, 2024 12:42 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
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?
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?
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
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
thank you for your feedback!
P.S. This point is explained in detail in https://amzn.to/3JHjZQv
-
- Posts: 4
- Joined: Thu Jan 02, 2025 12:25 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
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.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.
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?
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1273 :
The book is correct. This statement should be fixed.
thank you for your feedback!
thank you for your feedback!
Who is online
Users browsing this forum: No registered users and 12 guests