如果抛出异常,我期望缓冲读取器和文件读取器关闭并释放资源。
public static Object[] fromFile(String filePath) throws FileNotFoundException, IOException
{
try (BufferedReader br = new BufferedReader(new FileReader(filePath)))
{
return read(br);
}
}
但是,是否需要有一个catch
成功关闭的条款?
编辑:
本质上,Java 7 中的上述代码是否等同于 Java 6 中的以下代码:
public static Object[] fromFile(String filePath) throws FileNotFoundException, IOException
{
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(filePath));
return read(br);
}
catch (Exception ex)
{
throw ex;
}
finally
{
try
{
if (br != null) br.close();
}
catch(Exception ex)
{
}
}
return null;
}