About Question enthuware.ocpjp.v7.2.1240 :

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

Moderator: admin

Post Reply
thodoris.bais
Posts: 25
Joined: Sat Jun 03, 2017 4:56 pm
Contact:

About Question enthuware.ocpjp.v7.2.1240 :

Post by thodoris.bais »

Code: Select all

Any exception that is thrown while closing a resource is added to the list of suppressed exceptions of the exception thrown while opening a resource (or thrown from the try block.)
That said, suppressed exceptions do not prohibit the program from continuing its normal execution.

enthunoob
Posts: 57
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

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

Post by enthunoob »

The close method for D2 wasn't called ('Closing device D2' was not printed), because the constructor method of D2 did not fully finish. Is this assumption correct?

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

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

Post by admin »

Correct.

dchmielewski
Posts: 1
Joined: Sat Apr 05, 2025 2:53 am
Contact:

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

Post by dchmielewski »

if the program throw new RuntimeException in the close() method when creating Device d1, wouldn't it terminate the program with this RuntimeException?
not sure why throw new RuntimeException is ignored even though it should be executed right after "Closing device D1" statement.

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

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

Post by admin »

No, the program doesn't throw any exception while creating Device d1. There is no exception during new Device("D1"). Device D1 is created successfully. The program throws an IOException when creating d1 because new Device("D2") throws an IOException. When this exception is thrown, the try-with-resources mechanism will try to close Device D1 and that is when a RuntimeException is thrown.

Please go through the explanation carefully, it doesn't say that new RuntimeException is ignored. The explanation says it is added to the list of suppressed exceptions.

You can run the program and check.

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests