LS, In my opinion an IOException has been thrown by the method writeHeader(). Therefore the catch is reached before even applying for the d.close(). That's the reason why I choose "Device Opened Got Exception" as right answer for this question. Can you please explain on what point my thoughts went wrong?
Thanks a lot!
Greetings, Anneke
enthuware.ocpjp.v7.2.1208
Moderator: admin
-
- Posts: 2
- Joined: Thu Aug 24, 2017 3:34 am
- Contact:
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v7.2.1208
The d.close() statement that is written explicitly in the given code is NOT what is causing "Device closed" to be printed.
The close method on d is being called because of the try-with-resource statement trying to close the resource pointed to by d. This call is causing "Device closed" to be printed before "Got Exception".
The close method on d is being called because of the try-with-resource statement trying to close the resource pointed to by d. This call is causing "Device closed" to be printed before "Got Exception".
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Mon Jan 14, 2019 11:16 pm
- Contact:
Re: enthuware.ocpjp.v7.2.1208
Are d.read() and d.writeHeader("TEST") suppressed and only close is called?
I really don't understand how the flow goes here, like which gets called when something is executed.
I really don't understand how the flow goes here, like which gets called when something is executed.
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v7.2.1208
The call to d.read() throws an exception so the next line i.e. d.writeHeader("TEST") will not be executed. The control will go to catch block, but before going to the catch block, it has to close the resource that was opened in the try block. Therefore, Device Closed will be printed before Got Exception.
You may want to go through the following articles on this topic to learn the basics:
https://www.javacodegeeks.com/2011/07/j ... ained.html
http://tutorials.jenkov.com/java-except ... urces.html
You may want to go through the following articles on this topic to learn the basics:
https://www.javacodegeeks.com/2011/07/j ... ained.html
http://tutorials.jenkov.com/java-except ... urces.html
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Sat Feb 15, 2020 12:44 am
- Contact:
Re: enthuware.ocpjp.v7.2.1208
Maybe you could print your last post as explanation to the correct answer (none is shown so far). Thanks!
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocpjp.v7.2.1208
The following explanation is shown below the correct option:
Catch and finally blocks are executed after the resource opened in try-with-resources is closed. Therefore, Device Closed will be printed before Got Exception.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 39 guests