On Tue, 19 Sep 2023 16:10:38 GMT, Alexey Ivanov <[email protected]> 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

Reply via email to