Page 1 of 1

enthuware.ocpjp.v7.2.1208

Posted: Thu Aug 24, 2017 3:42 am
by Anneke71
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

Re: enthuware.ocpjp.v7.2.1208

Posted: Thu Aug 24, 2017 4:18 am
by admin
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".

Re: enthuware.ocpjp.v7.2.1208

Posted: Fri Jan 25, 2019 5:21 pm
by AungKNyar
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.

Re: enthuware.ocpjp.v7.2.1208

Posted: Fri Jan 25, 2019 10:37 pm
by admin
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

Re: enthuware.ocpjp.v7.2.1208

Posted: Tue Feb 18, 2020 6:21 am
by bvrulez
Maybe you could print your last post as explanation to the correct answer (none is shown so far). Thanks!

Re: enthuware.ocpjp.v7.2.1208

Posted: Tue Feb 18, 2020 7:10 am
by admin
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.