| 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
|
|
Correct. The entire java.nio.file package, which includes NIO2, was added in Java 7.
|
 |
 |
|
|
subject: java.io.FileNotFoundException versus java.nio.file.NoSuchFileException
|
|
|