Do you agree that the designers of Java class java.io.IOException
should have made it an unchecked run-time exception derived from java.lang.RuntimeException<
I completely disagree. To me the model is correct. A RuntimeException is one which most typically denotes a serious error in the logic of the programming (such as ArrayIndexOutOfBounds, NullPointer, or IllegalArgument) or something that the runtime has otherwise determined really shouldn't be happening (such as SecurityException).
Conversely IOException and its derivatives are exceptions that could reasonably occur during normal execution of a program, and common logic would dictate that either those problems should be dealt with, or at least the programmer should be aware that they can occur. For example with Files if your application logger can't write its data would you rather be forced to catch a potential IOException and recover, or have something that may not be critical to your app bring down the whole JVM because no one thought to catch the unchecked Exception (as you may have guessed, I'll choose the former).
I think that there are many situations in which an IOException is either recoverable, or at the least the programmer should be explicitly aware of the potential so that if it is not recoverable the system might be able to crash more "gently".
As far your thought of if the system can not recover there are always alternatives with a checked exception. You can always have your methods declare it in their throws, throw a runtime exception of their own or crash the JVM violently:
public void doit() throws IOException {
try{
}catch(IOException e){
// try to recover
...
// can't recover
throw e;
}
}
public void doit() {
try{
}catch(IOException e){
// try to recover
...
// can't recover
throw new RuntimeException(e);
}
}
public void doit() {
try{
}catch(IOException e){
// try to recover
...
// OH NO!!!!
System.exit(Constant.UNRECOVERABLE_IO_ERROR);
}
}