• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is FileNotFoundException not a RuntimeException?  RSS feed

 
Michael Golightly
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question that has been bugging me a bit recently, and I'm wondering why the java FileNotFoundException isn't a RuntimeException?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it doesn't indicate an error in your code (RuntimeException) nor a severe internal problem in the JVM (Error). It's not something you can prevent by coding, and when it happens, it would be reasonable and normal to try to recover from it. Hence, it is a checked exception.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think of RuntimeExceptions as very unexpected conditions. They could be caused by bugs, like dereferencing a null pointer, going outside a collection index, dividing by zero, etc. Or they could by caused by external system issues like running out of memory or other resources. In most cases, the handling of these exceptions is going to be log it and let the process die. That process could be a request, a thread, or the entire program. Trying to access a file that's not there isn't in the same category. You'd expect to run into that at least once in a while, and should have a proper handler for it.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually don't agree 100%. A FileNotFoundException is usually thrown for one of two reasons: a file does not exists, or you don't have enough rights to access the file. Both can actually be verified first, using exists() and canRead() / canWrite(). Therefore, it should be possible to prevent all occurrences of FileNotFoundException - unless it's misused (as throwing it for file access problems already is).
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: Therefore, it should be possible to prevent all occurrences of FileNotFoundException - unless it's misused (as throwing it for file access problems already is).



Not neccesarily. The File can dissapear between the time you check it's there and when you open it. Although it's a good idea to do File.exists first, you still need to be able to recover from the condition that someone deletes the file right after you call exists.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:I actually don't agree 100%. A FileNotFoundException is usually thrown for one of two reasons: a file does not exists, or you don't have enough rights to access the file. Both can actually be verified first, using exists() and canRead() / canWrite().


But both can change between those calls and when you actually try to read/write it. It's just like checking if a network host is reachable before trying to connect to it. Regardless of the results of the outcome, the actual operation can still fail, and you have to be able to handle that, so there's little or no point in checking first. Just assume that things are as they should be, proceed on that assumption, and handle the exception if/when it arises. That's kind of the key point to the whole exception mechanism in the first place--it lets you write happy-path code on the assumption it will succeed, detect and handle the exceptional case of it failing in a separate code path.

 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:. . . a file does not exists, or you don't have enough rights to access the file. . . .
What if you have access to the directory but don’t have rights to create a new file?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That shouldn't throw a FileNotFoundException but some other IOException. Throwing a FileNotFoundException is abusing that class as the problem clearly isn't a file not being found.
But I forgot the case where an external application (i.e. something not under the programmer's control, unlike other threads) can invalidate the result of an exists() check.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!