RFR: 8315952: Open source several Swing JToolbar JTooltip JTree tests [v2]
Harshitha Onkar
honkar at openjdk.org
Tue Sep 19 16:35:53 UTC 2023
On Tue, 19 Sep 2023 16:10:38 GMT, Alexey Ivanov <aivanov at openjdk.org> wrote:
>> test/jdk/javax/swing/JToolBar/bug4368050.java line 48:
>>
>>> 46: ObjectInputStream ois = new ObjectInputStream(bais)) {
>>> 47: ois.readObject();
>>> 48: }
>>
>> Better to close the stream objects in finally block in case of Exception.
>>
>>
>> finally {
>> if (oos != null) {
>> oos.close();
>> }
>> if (ois != null) {
>> ois.close();
>> }
>> }
>
>> Better to close the stream objects in finally block in case of Exception.
>>
>> ```
>> finally {
>> if (oos != null) {
>> oos.close();
>> }
>> if (ois != null) {
>> ois.close();
>> }
>> }
>> ```
>
> No, *it's not better.* If `oos.close()` throws an exception, then `ois` is left open. In addition to that, any exception thrown from the finally block will replace the exception thrown from the try block.
>
> *The best way is using [**try-with-resources**](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)*: it handles all the above correctly.
Noted. Thanks!
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/15755#discussion_r1330408622
More information about the client-libs-dev
mailing list