Reputation: 9010
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(); }
}
}
}
Upvotes: 1
Views: 1097
Reputation: 6824
FIleNotFoundException is a checked exception. You need to handle these type of exceptions if your codeblock is throwing them. For unchecked type of exceptions, you don't have to handle (not mandatory). Can you really guarantee that the file is created, but not corrupted or the disk record isn't corrupted? Also, look into this - Java: checked vs unchecked exception explanation.
Main thing is that your constructor is throwing FileNotFoundException (look here - https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#print%28char%29), which you have to catch (because it is checked).
Tips - For Eclipse, try and see what ctrl+SPACe reveals about an object's method. If your JavaDoc is in the right location, you will see all the explanation of what a method is doing including "Throws: SomeException" bit. This is what you need to look for when calling a method (i.e. whether you need try catch block for this).
Upvotes: 1
Reputation: 5758
FileNotFoundException
is a checked exception, which simply translates that you will have to either catch it or add it in the throws clause.
I hope this answers your question about why we actually need it even though a file is created if not present-
From javadoc -
FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
Upvotes: 3
Reputation: 200148
Replace Eclipse Workspaces
in your path with foo
and see if you get the exception. The file itself may be created, but not the whole path above it.
You can also leave the path exactly as it is, but set read-only, hidden, and system attributes on the file. The OS will not be able to either write or create it.
Another variation: modify the file's ACL so your user doesn't have the write permission.
There are many more.
Upvotes: 2