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
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply 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: 13842

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
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18368

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.
 
IntelliJ Java IDE
 
subject: java.io.FileNotFoundException versus java.nio.file.NoSuchFileException
 
Threads others viewed
FileReader question
Site valid or not?
FileNotFoundException
how to select root cause of exception from nested exception
JSP File Not Found
MyEclipse, The Clear Choice