Is there a system indipendent way of detecting when an IOException
is related to a "no space left on device" error? IOException
has no methods. Shall I treat it as a generic I/O error without explaining the user why? Or should I simply display the exception message string to the user (which might be too techinical)?
Asked
Active
Viewed 2,378 times
4

Daniele Ricci
- 571
- 1
- 7
- 28
2 Answers
1
Java, differently from other languages like C#, does not keep track of the cause of the IOException
, but it uses subclasses to better specify the motivation of the exception, for example FileNotFoundException
is a subclass of IOException
.
Generally is a good idea to specify the cause for the most common sublcasses, and prefix the error message for IOException
with a generic description to facilitate the user.
try {
...
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} catch (.. other subclasses ..) {
...
} catch (IOException e) { // anything else
System.out.println("I/O Exception: " + e.getMessage());
e.printStackTrace();
}

Community
- 1
- 1

enrico.bacis
- 30,497
- 10
- 86
- 115
1
As part of exception handling, you can check for the available disk space using these methods provided by the File class:
public long getTotalSpace()
public long getFreeSpace()
public long getUsableSpace()
More details can be found in this related question: How to find how much disk space is left using Java?
-
I don't know... it might not be reliable. There are a ton of reasons possible for a no space left error, such as quota exceeded. And on ext2/3/4 file systems you can reserve some blocks for the superuser. – Daniele Ricci Jul 19 '15 at 13:55