. Advertisement .
..3..
. Advertisement .
..4..
Here is the program I run:
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();
}
}
After I run, it returns an 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.
Does anyone have any suggestions for the problem below: unreported exception filenotfoundexception; must be caught or declared to be thrown in the java- How to correct it?
The cause: I think this error happens because you are not informing the compiler that there is a possibility of throwing a
FileNotFoundException
.If the file does not exist, aFileNotFoundException
will be thrown.The solution: To solve this problem, you can try:
This is a basic guide for those who are new to Java and want to learn
PrintWriter
.