• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jessie Kensinger
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic