Detection on device no error left

Is there a system of independent way of detecting when it IOException

is associated with the error "No space on the device"? IOException

has no methods. Should I treat it as a generic I / O error without explaining why? Or should I just display the exception message string to the user (which might be too technologically advanced)?

+3


source to share


2 answers


Java, unlike other languages ​​like C # , does not track the cause IOException

, but uses subclasses to better define the motivation for an exception, such FileNotFoundException

as subclassing IOException

.

It is generally recommended that you provide a reason for the most common subclasses and prefix the error message for IOException

a generic description to make it easier for 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();
}  

      

+2


source


As part of exception handling, you can check the available disk space using these methods provided by the File class:

public long getTotalSpace()
public long getFreeSpace()
public long getUsableSpace()

      



More information can be found in this related question: How to determine how much disk space is left using Java?

+1


source







All Articles