我已经多次检查了这段代码,但我无法弄清楚为什么它会吐出关于 catch 语句的错误。我知道使用 Java 7 可以使用一个 catch 子句处理多个异常。
import java.io.*;
import java.util.*;
public class MultiCatch
{
public static void main(String[] args)
{
int number;
try
{
File file = new File("Numbers.txt");
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
number = inputFile.nextInt();
System.out.println(number);
}
inputFile.close();
}
catch(FileNotFoundException | InputMismatchException ex)
{
System.out.println("Error processing the file.");
//System.out.println("Error processing the file." + ex.getMessage());
}
}
}
错误:
$ javac MultiCatch.java
MultiCatch.java:25: <identifier> expected
catch(FileNotFoundException | InputMismatchException ex)
^
MultiCatch.java:25: '{' expected
catch(FileNotFoundException | InputMismatchException ex)
^
MultiCatch.java:25: not a statement
catch(FileNotFoundException | InputMismatchException ex)
^
MultiCatch.java:25: ';' expected
catch(FileNotFoundException | InputMismatchException ex)
^
MultiCatch.java:31: reached end of file while parsing
}
^
5 errors
如果它有所作为,我在运行 Java 7 的 OSX 10.8 上。