简体   繁体   English

此语言级别不支持多次捕获

[英]Multi-catches are not supported a this language level

So I want to type a number, and then a name of a person or something else. 所以我想输入一个数字,然后输入一个人或其他人的名字。 There is no problem with that, but why can't I put 2 error exceptions in 1 block? 这没有问题,但为什么我不能在1个块中放置2个错误异常?

while (true) {
    try {
        int id = Integer.parseInt( reader.readLine() );
        String name = reader.readLine();

        if (name.equals("")) {
            break;
        }

        map.put(name, id);
    } catch (NumberFormatException | IOException e) {
        break;
    }
}

When I'm trying to print my value, I get NumberFormatException 当我试图打印我的值时,我得到NumberFormatException

for (Map.Entry<Integer, String> pair: map.entrySet()) {
    int id = pair.getKey();
    String name = pair.getValue();
    System.out.println(id + " " + name);
}

The problem is not in the code, but in the compiler level. 问题不在代码中,而是在编译器级别。 Just check your IDE settings and set the compiler level to 1.7 or higher. 只需检查IDE设置并将编译器级别设置为1.7或更高。

If you are using IntelliJ and set it to 7.0 properly in the following path and still didn't work: 如果您使用IntelliJ并在以下路径中正确设置为7.0,但仍然无法正常工作:

File -> Project Structure -> Project settings -> Project -> Project language level 文件 - >项目结构 - >项目设置 - >项目 - >项目语言级别

Set in the module: 在模块中设置:

File -> Project Structure -> Project settings -> Modules -> Project language level 文件 - >项目结构 - >项目设置 - > 模块 - > 项目语言级别

In my case, an explicit configuration in my pom.xml did the trick. 就我而言,我的pom.xml的显式配置就可以了。 The IntelliJ setting in File -> Project Structure -> Project -> Project language level was set to my JDK 1.7 but the IDE kept ignoring the setting. 文件 - >项目结构 - >项目 - >项目语言级别中的IntelliJ设置已设置为我的JDK 1.7,但IDE一直忽略该设置。

I used the Maven Compiler Plugin to specify the source and target version as 1.7 我使用Maven编译器插件将源版本和目标版本指定为1.7

To do so, in the <plugins> section, add the following lines: 为此,请在<plugins>部分中添加以下行:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
</plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM