enthuware.ocpjp.v7.2.1208

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Anneke71
Posts: 2
Joined: Thu Aug 24, 2017 3:34 am
Contact:

enthuware.ocpjp.v7.2.1208

Post 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

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

Re: enthuware.ocpjp.v7.2.1208

Post 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".
If you like our products and services, please help us by posting your review here.

AungKNyar
Posts: 5
Joined: Mon Jan 14, 2019 11:16 pm
Contact:

Re: enthuware.ocpjp.v7.2.1208

Post 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.

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

Re: enthuware.ocpjp.v7.2.1208

Post 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
If you like our products and services, please help us by posting your review here.

bvrulez
Posts: 33
Joined: Sat Feb 15, 2020 12:44 am
Contact:

Re: enthuware.ocpjp.v7.2.1208

Post by bvrulez »

Maybe you could print your last post as explanation to the correct answer (none is shown so far). Thanks!

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

Re: enthuware.ocpjp.v7.2.1208

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 39 guests