Using FileReader causes a compiler error “unhandled exception type FileNotFoundException”

后端 未结 6 1020
傲寒
傲寒 2021-01-15 10:01

Ive read a few threads here that relate the same problem, but the solutions arent working. :/

I use Eclipse, here is my program.

package mypackage;
         


        
6条回答
  •  滥情空心
    2021-01-15 10:45

    Unhandled exception type FileNotFoundException myclass.java /myproject/src/mypackage

    This is a compiler error. Eclipse is telling you that your program does not compile to java byte code (so of course you can't run it). For now, you can fix it by simply declaring that your program may throw this exception. Like so:

    public static void main(String[] args) throws FileNotFoundException {
    

    FileNotFoundException is a "checked exception" (google this) which means that the code has to state what the JVM should do if it is encountered. In code, a try-catch block or a throws declaration indicate to the JVM how to handle the exception.

    For future reference, please note that the red squiggly underline in Eclipse means there is a compiler error. If you hover the mouse over the problem, Eclipse will usually suggest some very good solutions. In this case, one suggestion would be to "add a throws clause to main".

提交回复
热议问题