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

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
    }
}

Leave a Comment

error code: 523