In the following SSCCE, I do not get a FileNotFoundException
even if I delete this file from the given location/path i.e. "D:\\Eclipse Workspaces\\SAMPLES AND OTHER SNIPPETS\\SoapCallResults.txt"
Rather the PrintWriter
seems to create the file if it is not found.
If the Printwriter
creates the file if it is not found, why do we try to handle the FileNotFoundException
(compiler complains if we don't surround it with try/catch
or add a throws
clause) when it is never going to be thrown?
package com.general_tests;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class PrintWriterFileNotFoundExceptionTest {
public static void main(String[] args) {
String myName = "What ever my name is!";
PrintWriter printWriter = null;
try {
printWriter = new PrintWriter("D:\\Eclipse Workspaces\\SAMPLES AND OTHER SNIPPETS\\SoapCallResults.txt");
printWriter.println(myName);
} catch (FileNotFoundException e) {
System.out.println("FILE NOT FOUND EXCEPTION!");
e.printStackTrace();
} finally {
if (printWriter != null) { printWriter.close(); }
}
}
}