我已經檢查了幾次這個代碼,我不知道爲什麼它隨時會拋出錯誤關於catch語句。我知道,處理多個異常的一個catch子句可以與Java 7Java多捕獲異常錯誤
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。
你的JRE可能是Java 7,但你很可能在Java 6下編譯。第一個錯誤是期望一個標識符,它將表明它正在尋找在java 6規範下允許的'catch(FileNotFoundException ex)'。 – 2013-05-07 20:05:03
你的IDE是什麼? – 2013-05-07 23:16:27
@CsabaToth我正在使用命令行界面編譯我的代碼。 – hfranco 2013-05-08 15:03:01