throws Exception in finally blocks

后端 未结 15 2327
慢半拍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:35

    If you're using Java 7, and resource implements AutoClosable, you can do this (using InputStream as an example):

    try (InputStream resource = getInputStream()) {
      // Use the resource.
    }
    catch( Exception ex ) {
      // Problem with the resource.
    }
    

提交回复
热议问题