error: unreported exception FileNotFoundException; must be caught or declared to be thrown

为君一笑 提交于 2019-12-28 06:32:05

问题


I'm trying to create a simple program that will output a string to a text file. Using code I found here, I have put together the following code:

import java.io.*;

public class Testing {

  public static void main(String[] args) {

    File file = new File ("file.txt");
    file.getParentFile().mkdirs();

    PrintWriter printWriter = new PrintWriter(file);
    printWriter.println ("hello");
    printWriter.close();       
  }
} 

J-grasp throws me the following error:

 ----jGRASP exec: javac -g Testing.java

Testing.java:10: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
    PrintWriter printWriter = new PrintWriter(file);
                              ^
1 error

 ----jGRASP wedge2: exit code for process is 1.

Since I'm pretty new to Java, I have no idea what this means. Can anybody point me in the right direction?


回答1:


You are not telling the compiler that there is a chance to throw a FileNotFoundException a FileNotFoundException will be thrown if the file does not exist.

try this

public static void main(String[] args) throws FileNotFoundException {
    File file = new File ("file.txt");
    file.getParentFile().mkdirs();
    try
    {
        PrintWriter printWriter = new PrintWriter(file);
        printWriter.println ("hello");
        printWriter.close();       
    }
    catch (FileNotFoundException ex)  
    {
        // insert code to run when exception occurs
    }
}



回答2:


a PrintWriter might throw an exception if there is something wrong with the file, like if the file doesn't exist. so you have to add

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

then it will compile and use a try..catch clause to catch and process the exception.




回答3:


If you're very new to Java, and just trying to learn how to use PrintWriter, here is some bare-bones code:

import java.io.*;

public class SimpleFile {
    public static void main (String[] args) throws IOException {
        PrintWriter writeMe = new PrintWriter("newFIle.txt");
        writeMe.println("Just writing some text to print to your file ");
        writeMe.close();
    }
}



回答4:


This means that when you call new PrintWriter(file), it can throw an exception. You either need to handle that exception, or make your program able to rethrow it.

import java.io.*;

public class Testing {

    public static void main(String[] args) {

    File file = new File ("file.txt");
    file.getParentFile().mkdirs();

    PrintWriter printWriter;
    try {
        printwriter = new PrintWriter(file);
        printWriter.println ("hello");
        printWriter.close();
    } catch (FileNotFoundException fnfe) {
        // Do something useful with that error
        // For example:
        System.out.println(fnfe);
    }
}


来源:https://stackoverflow.com/questions/19788989/error-unreported-exception-filenotfoundexception-must-be-caught-or-declared-to

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!