try catch finally 关闭流标准的写法

平常开发中,都知道要在finlly里关闭流,但是有时finlly里代码不当,会引起另外的异常。 
以下是看struts2源代码看到的,随手记录下。 
有两点注意: 
(1)判断流是否为空。 
(2)filly里要捕获异常 

Java代码  收藏代码
    1.         InputStream in = null;  
    2.         try {  
    3.             in = settingsUrl.openStream();  
    4.             settings.load(in);  
    5.         } catch (IOException e) {  
    6.             throw new StrutsException("Could not load " + name + ".properties:"  
    7.                     + e, e);  
    8.         } finally {  
    9.             if (in != null) {  
    10.                 try {  
    11.                     in.close();  
    12.                 } catch (IOException io) {  
    13. //log  
    14.                 }  
    15.             }  
    16.         }  
posted @ 2016-04-11 14:40  系统攻城狮  阅读(1066)  评论(0编辑  收藏  举报