throws Exception in finally blocks

后端 未结 15 2324
慢半拍i
慢半拍i 2020-11-29 18:04

Is there an elegant way to handle exceptions that are thrown in finally block?

For example:

try {
  // Use the resource.
}
catch( Excep         


        
15条回答
  •  失恋的感觉
    2020-11-29 18:33

    Changing Resource from best answer to Closeable

    Streams implements Closeable Thus you can reuse the method for all streams

    protected void closeQuietly(Closeable resource) {
        if (resource == null) 
            return;
        try {
            resource.close();
        } catch (IOException e) {
            //log the exception
        }
    }
    

提交回复
热议问题