您好我们可以在Java 7中同时使用资源和多重捕获吗?我试图使用它,它给出了编译错误.我可能错误地使用它.请指正.
try(GZIPInputStream gzip = new GZIPInputStream(new FileInputStream(f));
BufferedReader br = new BufferedReader(new InputStreamReader(gzip))
{
br.readLine();
}
catch (FileNotFoundException | IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
是! 您可以.
但是,你的问题是在FileNotFoundException用IOException.因为FileNotFoundException是子类IOException,无效.仅IOException在catch块中使用.您还错过了)try语句中的右括号.这就是为什么,你有错误.
try(GZIPInputStream gzip = new GZIPInputStream(new FileInputStream(f));
BufferedReader br = new BufferedReader(new InputStreamReader(gzip)))
{
br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)