差異處
這裏顯示兩個版本的差異處。
下次修改 | 前次修改 | ||
java:basic:exception:try-with-resource [2016/05/03 20:16] tony 建立 |
java:basic:exception:try-with-resource [2023/06/25 09:48] (目前版本) |
||
---|---|---|---|
行 1: | 行 1: | ||
{{tag>java}} | {{tag>java}} | ||
- | ====== Try-With-Resource ====== | + | ====== Try-With-Resources ====== |
===== Introduction ===== | ===== Introduction ===== | ||
+ | 在操作與資源有關的API時,常常有人會忘記做close的動作,而造成memory-leak。而資源相關的API常常會伴隨著check-exception,我的習慣會在finally去做close的動作(可參考[[java:basic:memoryleakwithproperties|此篇]])。今天剛好看到同事用了try-with-resource寫法,才知道這種寫法,可以避免掉忘記關閉資源的問題。 | ||
+ | ===== How to? ===== | ||
+ | 以我的例子來示範該如何修改: | ||
+ | <code java> | ||
+ | private void close(Closeable closable){ | ||
+ | try { | ||
+ | if( closable != null ) | ||
+ | closable.close(); | ||
+ | } catch (IOException e) { | ||
+ | // log | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private void load(){ | ||
+ | mProp = new Properties(); | ||
+ | InputStream is = null; | ||
+ | try { | ||
+ | is = new FileInputStream(mConfigFilePath); | ||
+ | mProp.load(is ); | ||
+ | } catch (IOException e) { | ||
+ | // need to handle .. | ||
+ | } finally { | ||
+ | close(is); | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | 使用try-with-resources: | ||
+ | <code java> | ||
+ | try( InputStream is = new FileInputStream(mConfigFilePath) ){ | ||
+ | mProp.load(is); | ||
+ | } catch (IOException e) { | ||
+ | // need to handle .. | ||
+ | } | ||
+ | </code> | ||
+ | 需要注意的是,使用這種方式try()內被assign的物件必須實做closable,它在發生exception或正常執行完成都會自動呼叫close。可以自行透過以下程式模擬: | ||
+ | <code java> | ||
+ | public class TestClosable implements Closeable { | ||
+ | |||
+ | @Override | ||
+ | public void close() throws IOException { | ||
+ | System.out.println("close"); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | public class TryWithResourcePractice { | ||
+ | |||
+ | @Test | ||
+ | public void testCatch(){ | ||
+ | try( TestClosable tc = new TestClosable() ){ | ||
+ | System.out.println("gg"); | ||
+ | throw new IOException(); | ||
+ | } catch (IOException e) { | ||
+ | System.out.println("tt"); | ||
+ | } finally { | ||
+ | System.out.println("yy"); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </code> | ||
===== Resource ===== | ===== Resource ===== | ||
* [[http://stackoverflow.com/questions/17739362/java7-try-with-resources-statement-advantages|java7-try-with-resources-statement-advantages]] | * [[http://stackoverflow.com/questions/17739362/java7-try-with-resources-statement-advantages|java7-try-with-resources-statement-advantages]] | ||
* [[http://stackoverflow.com/questions/17650970/am-i-using-the-java-7-try-with-resources-correctly|am-i-using-the-java-7-try-with-resources-correctly]] | * [[http://stackoverflow.com/questions/17650970/am-i-using-the-java-7-try-with-resources-correctly|am-i-using-the-java-7-try-with-resources-correctly]] | ||
- | * [[http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.3|]] | + | * [[http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.3|Java7 - Try With Resources]] |