java try catch块的特殊语法 [英] Special syntax of java try catch block

查看:278
本文介绍了java try catch块的特殊语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Charset charset = Charset.forName("US-ASCII");
try (BufferedReader reader = Files.newBufferedReader(file, charset)) {
    String line = null;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException x) {
    System.err.format("IOException: %s%n", x);
}

我在 Java教程,其中括号语句位于关键字'try'之后。这有效吗?为什么不能eclipse识别它并报告语法错误?

I saw such a piece of code on The Java Tutorials, in which a bracket statement is after key word 'try'. Is this valid? why cannot eclipse recognize it and report syntax error?

推荐答案

这是Java 7的尝试使用资源功能。

It is the Java 7's try with resource feature.

你需要将Eclipse的编译器合规性级别设置为1.7,以便识别语法。

You need to set Eclipse's Compiler Compliance level to 1.7 for it to recognize the syntax.

您可以在窗口>下全局设置它。偏好> Java>编译器或者只是右键单击项目并执行如下所示:

You can either set it globally under Window > Preferences > Java > Compiler or simply right-click on the project and do as shown below:

这篇关于java try catch块的特殊语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆