这看起来几乎很傻,关闭OutputStream时最可靠的模式是什么? 现在我有以下try-catch-finally-overkill这样的东西。
专用流内容文件(output stream ostream,Properties contents ) )。
try {
contents.store(ostream,' comments ' );
}
catch(ioexceptione
thrownewresourceexception (' failedtowritecontents ',e );
}
法利{
try {
ostream.close (;
}
catch(ioexceptione ) { /* what can be done here anyway? () ) }
}
}
为什么会抛出被检查的异常对我来说仍然是个谜。 虽然可以创建一个关闭/捕获块的封闭方法,但是如果其中已经有类似file util.closefileandthrowuncheckedexception ()的内容,则希望使用它悲伤的时候有很多小项目,有很多开发者的时候会有帮助; 是正确的方法。