Java PrintWriter: Why do we handle FileNotFoundException if the file is automatically created if not found? -
in following sscce, not filenotfoundexception
if delete file given location/path i.e. "d:\\eclipse workspaces\\samples , other snippets\\soapcallresults.txt"
rather printwriter
seems create file if not found.
if printwriter
creates file if not found, why try handle filenotfoundexception
(compiler complains if don't surround try/catch
or add throws
clause) when never going 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 name is!"; printwriter printwriter = null; try { printwriter = new printwriter("d:\\eclipse workspaces\\samples , other snippets\\soapcallresults.txt"); printwriter.println(myname); } catch (filenotfoundexception e) { system.out.println("file not found exception!"); e.printstacktrace(); } { if (printwriter != null) { printwriter.close(); } } } }
replace eclipse workspaces
in path foo
, see if exception. file may created, not whole path above it.
you can leave path is, set read-only, hidden, , system attributes on file. os not able either write or create it.
another variation: modify file's acl user doesn't have write permission.
there many more.
Comments
Post a Comment