Java非常简单'抓住'没有'尝试'错误

时间:2017-10-23 20:32:31

标签: java

两个错误catch没有try try没有catchfinally或资源声明

    try {
        File file = new File("path to file");
        Scanner sc = new Scanner(file);
        String line;
        while (sc.hasNextLine()) {
            line = scanner.nextLine();
            System.out.println(line);
        } catch (FileNotFoundException e) {
            System.out.println("Error in input/output");
        }
        scanner.close();
    }       
  }
}                            

1 个答案:

答案 0 :(得分:2)

catch块必须是try块的 ,不在内部:

 try {
        File file = new File("path to file");
        Scanner sc = new Scanner(file);
        String line;
        while (sc.hasNextLine()) {
            line = scanner.nextLine();
            System.out.println(line);
        }
} catch (FileNotFoundException e) {
            System.out.println("Error in input/output");
} finally {
        scanner.close();
}