Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should class IOException in Java have been an unchecked RuntimeException?

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 instead of a checked exception derived only from java.lang.Exception?

I think that class IOException should have been an unchecked exception because there is little that an application can do to resolve problems like file system errors. However, in When You Can't Throw An Exception, Elliotte Rusty Harold claims that most I/O errors are transient and so you can retry an I/O operation several times before giving up:

For instance, an IOComparator might not take an I/O error lying down, but — because many I/O problems are transient — you can retry a few times, as shown in Listing 7:

Is this generally the case? Can a Java application correct I/O errors or wait for the system to recover? If so, then it is reasonable for IOException to be checked, but if it is not the case, then IOException should be unchecked so that business logic can delegate handling of this exception to a separate system error handler.

like image 854
Derek Mahar Avatar asked Apr 12 '10 17:04

Derek Mahar


People also ask

Is Java IOException checked or unchecked?

Because IOException is a checked exception type, thrown instances of this exception must be handled in the method where they are thrown or be declared to be handled further up the method-call stack by appending a throws clause to each affected method's header.

Is IOException a RuntimeException?

For example, IOException is a subclass of Exception and NullPointerException is a subclass of RuntimeException . You may have noticed that Java differentiates errors from exceptions.

Is IO exception unchecked exception in Java?

No because you can recover from some IOExceptions.

Is IO exception runtime?

Because IOException is a Checked Exception, which should be either handled or declared to be thrown. On contrary, RuntimeException is an Unchecked Exception.


1 Answers

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);   } } 
like image 79
M. Jessup Avatar answered Sep 22 '22 05:09

M. Jessup