This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes java.io.FileNotFoundException versus java.nio.file.NoSuchFileException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "java.io.FileNotFoundException versus java.nio.file.NoSuchFileException" Watch "java.io.FileNotFoundException versus java.nio.file.NoSuchFileException" New topic
Author

java.io.FileNotFoundException versus java.nio.file.NoSuchFileException

Jessie Kensinger
Greenhorn

Joined: Sep 27, 2011
Posts: 8
java.io.FileNotFoundException and java.nio.file.NoSuchFileException

looks to me like NoSuchFileException is new to java 7?

by looking at the documentation, it seems to me like FileNotFoundException alludes to failure to open a file, where NoSuchFileException could be applied in more general circumstances.

for people who are "lazy" and like to re-use built in exceptions, any thoughts or feelings as too why one would switch to using NoSuchFileException?

i am a bit confused why there couldnt just be one "file was not found exception" in the language...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Jessie Kensinger wrote:by looking at the documentation, it seems to me like FileNotFoundException alludes to failure to open a file, where NoSuchFileException could be applied in more general circumstances.


No, I think it's the other way around. The Java 7 exception NoSuchFileException is used only when the requested file does not exist; the earlier version FileNotFoundException was used for that and also for cases where the file does exist but can't be accessed for some reason. In Java 7 there is AccessDeniedException for the latter cases.

for people who are "lazy" and like to re-use built in exceptions, any thoughts or feelings as too why one would switch to using NoSuchFileException?


Perhaps the language designers decided that reusing exceptions was actually a bad thing (although being lazy is usually a good characteristic for a programmer). The old FileNotFoundException was even thrown when a URLConnection failed to access an HTTP resource -- this wasn't helpful when the exception was read by somebody who wasn't too clear on the difference between a file and a resource.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Jessie Kensinger wrote:looks to me like NoSuchFileException is new to java 7?

Correct. The entire java.nio.file package, which includes NIO2, was added in Java 7.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.io.FileNotFoundException versus java.nio.file.NoSuchFileException
 
Similar Threads
JSP File Not Found
how to select root cause of exception from nested exception
FileReader question
FileNotFoundException
Site valid or not?