views:

59

answers:

3

Is there a way to get from the IOException object the file that fails?

+4  A: 

No, because IOException is a generic exception not necessarily related to any file at all. Do you mean any specific subclass of this exception?

koppernickus
What about if the IOExceptio is a instaceof FileNotFoundException?
Tom Brito
@Tom Brito: The docs say no: http://java.sun.com/javase/6/docs/api/java/io/FileNotFoundException.html A specific JVM may or may not include the filename in the message (e.g., in `getMessage`), but it's not *documented* as being the case.
T.J. Crowder
+2  A: 

IOException is thrown for much more than just file access. You can subclass IOException and throw that if you want, and you can attach arbitrary fields to the subclassed exception. From there, you would probably have to catch IOExceptions and rethrow them as IOExceptionWithFileInformation where appropriate.

Stefan Kendall
+1  A: 

Note that IOException instances are not necessarilly related to files. Furthermore, the ones referring specifically to files (e.g. FileNotFoundException) do not seem to have a field with the file path. You may extract the file name from the description.

Eyal Schneider