Page 1 of 1

About Question enthuware.ocpjp.v7.2.1240 :

Posted: Sun Aug 12, 2018 6:10 am
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.

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

Posted: Sat Apr 17, 2021 2:21 pm
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?

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

Posted: Sat Apr 17, 2021 3:10 pm
by admin
Correct.

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

Posted: Sat Apr 05, 2025 2:56 am
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.

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

Posted: Mon Apr 07, 2025 2:45 am
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.