简体   繁体   English

当我正在读取的文件存在时,Java程序会给我FileNotFoundException,但是如果我处理异常,则可以正常工作

[英]Java program gives me FileNotFoundException when the file I am reading from exists but then works perfectly fine if I handle the exception

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class WordJumble {

    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub
        File file = new File("F:/Files/Topic.txt");
        Scanner sc = new Scanner(file); 

        String title = sc.nextLine();
        System.out.println(title);
        for(int i=0;i<10;i++){
            System.out.println(sc.nextLine());
        }
    }
}

Currently the program does what I want it to, but why is it giving me an error about the file not existing? 目前,该程序可以实现我想要的功能,但是为什么会给我有关该文件不存在的错误的信息? When I add the throws clause to ignore the error it is able to find the file without issue. 当我添加throws子句以忽略该错误时,它便能够毫无问题地找到该文件。

While the wording of the error may be a little confusing, the error isn't a FileNotFoundException in and of itself but is instead a complaint that you aren't dealing with the possibility of such an exception being thrown. 尽管错误的措辞可能有点令人困惑,但该错误本身并不是FileNotFoundException,而是抱怨您没有处理抛出此类异常的可能性 All your compiler is telling you is that you need to deal with the possibility of the file not being where you think it is. 编译器告诉您的是,您需要处理该文件不在您认为的位置的可能性。 Therefore, when you add throws FileNotFoundException to the method signature the compiler is satisfied and your error goes away. 因此,当您向方法签名添加throws FileNotFoundException ,编译器将得到满足,并且错误将消失。

When you said 'add the statement to ignore the error' you meant adding the 'throws...' clause to the definition of main so it would compile cleanly. 当您说“添加语句以忽略错误”时,您的意思是在main的定义中添加“ throws ...”子句,以便可以进行干净地编译。 Right? 对?

What's going on is that Scanner many throw a FileNotFoundException if the file is not found. 发生的情况是,如果找不到该文件,许多Scanner抛出FileNotFoundException This exception must be handled (caught) somewhere. 必须在某个地方处理(捕获)此异常。

Instead, you elected not to handle, and said that it could propagate out from main . 相反,您选择不处理,并说它可以从main传播出去。

The appropriate way to do this is use a try - catch construction. 做到这一点的适当方法是使用try - catch结构。

try {
   Scanner sc = new Scanner(file);
   :
   :
catch (FileNotFoundException ex) {
   ... print an error or something ...
}

This sort of approach is used so that the error handling is 'out of line' of the main flow of the code. 使用这种方法是为了使错误处理在代码主流中“与众不同”。

暂无
暂无

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

相关问题 在没有ItemListener的情况下可以正常工作,但是当我添加它时,它给了我NullPointerException - Works fine without ItemListener, but when I add it, it gives me a NullPointerException 我的 java 编译器在命令提示符下处理我的程序,但是当我尝试运行该程序时,它给了我一个错误 - My java compiler works on my program in the command prompt but when i try to run the program it gives me an error 为什么从文件重定向输入而不是为Java程序手动输入时出现异常? - Why am I getting an exception when redirecting input from file, but not manual input for a Java program? 当我从终端手动运行Java程序时,它运行正常,但是当我从Eclipse运行时,它却无法运行 - When I am running java program manually from terminal it is working fine but it is not working when i am running from eclipse 从 src/main/resources 读取会在命令行上给出 java.nio.file.InvalidPathException (但在 Eclipse 中工作正常) - Reading from src/main/resources gives java.nio.file.InvalidPathException on command line (but works fine in Eclipse) 我正在尝试运行文件程序,出现错误FileNotFoundException - I am trying to run file program, I get error FileNotFoundException 我的链表(带有泛型)程序可以很好地编译,但是当我运行它时,却出现未找到类的异常。 有人能帮我吗? - My Linked List (with generics) program compiles fine, but when I run it I am getting a class not found exception. Can someone help me? 为什么在此程序中出现java.io.FileNotFoundException? - Why am I getting java.io.FileNotFoundException in this program? netbeans中的Java程序无法正常运行,但在调试时可以正常运行 - Java program in netbeans won't run properly but works perfectly fine when debugging 文件存在,但是当我在该文件上执行操作时,系统显示FileNotFoundException异常。 当文件位于源代码所在的目录中时有效 - File exists but when I perform operation on that file, system shows FileNotFoundException. Works when file is in dir where source code is
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM