Is it possible to get the error code returned by the operating system to the JVM when a FileNotFoundException is thrown?
The issue I’ve got is when trying to open an input file stream on a file when the file is locked by the OS causes a FileNotFoundException to be thrown even though the file actually does exist.
If I try to open the file using some code written in C, the operating system returns ERROR_SHARING_VIOLATION as defined in winerror.h. However in JAVA a FileNotFoundException is thrown with the error message “The process cannot access the file because it is being used by another process.”
What I’d like to do is add some logic to the try/catch to retry opening the file if certain specific errors are encountered but I can’t figure out how to drill into the FileNotFoundException and find the underlying cause of the error. The only other option I’ve got is checking the error message description but it just doesn’t seem the right way to do it.
Unfortunately no.
FileNotFoundExceptionhas no subclasses which refines it, and the class itself has not specific access-methods. This is a common problem and presumably due to the fact that the standard API is intended to be platform independent.The work-around is usually to parse the string returned by
getMessage()or to drop to native level and solve it in a platform specific way in for instance C.