Search code examples
javachecked-exceptions

relax exception catch necessity


Is there a possibility in Java to get rid of the necessity to catch non-RuntimeException exceptions? Maybe compiler flags?

I know the reason why the catching is promoted, but want to do simple and straight tools that enforce their requirements. So if something can went wrong I don't like to catch up but exit the application, crashing with a meaningful exception. Usually this ends up like:

try {
     connection.close();
} catch (IOException e) {
    throw new RuntimeException(e);
}   

which introduces 4 lines of code mess, and introduces the wrapping RuntimeException mess on error output. Sometimes it even motivate people to wrap large try ... catch (Throwable ..) blocks around anything, which is the likely cause for our beloved 'Unknown error occured' alert boxes...


Solution

  • Is there a possibility in Java to get rid of the necessity to catch non-RuntimeException exceptions?

    For a checked exception, you can chose between catching the exception and declaring it in the method header as thrown.

    Maybe compiler flags?

    No. There are no compiler flags to relax this. It is a fundamental part of the language design. Relaxing the checked exception rules via a compiler switch would cause serious library interoperability problems.