krosenvold
krosenvold

Reputation: 77231

Replace a checked exception with a runtime exception?

Given that I basically want to eliminate checked exception usage and transform them to runtime exceptions, I would normally be doing something like this:

try {
    file.read();
} catch (IOException e){
    throw new RuntimeException(e); 
}

There are several disadvantages to doing this, but the one that irritates me the most is that my runtime exception would contain a nested stacktrace. Basically I would like to re-throw the "IOException" as a RuntimeException (or "IORuntimeException") with the original message and stacktrace, so I can avoid the useless nested stacktrace. The "fact" that I have re-thrown the exception somewhere in the middle seems like just useless noise to me.

Is this possible ? Is there any library that does this ?

Upvotes: 10

Views: 2807

Answers (6)

AlikElzin-kilaka
AlikElzin-kilaka

Reputation: 36051

As of Java 8 there's another way:

try {
  // some code that can throw both checked and runtime exception
} catch (Exception e) {
  throw rethrow(e);
}

@SuppressWarnings("unchecked")
public static <T extends Throwable> RuntimeException rethrow(Throwable throwable) throws T {
    throw (T) throwable; // rely on vacuous cast
}

* More info here.

Upvotes: 0

Oliver Watkins
Oliver Watkins

Reputation: 13539

sounds like you actually NEED checked Exceptions

Upvotes: 0

artbristol
artbristol

Reputation: 32437

If you're considering the other answer's use of Unsafe (I recommend not, but anyway), another option is to abuse generics to throw a checked exception with this evil pair of methods (from http://james-iry.blogspot.co.uk/2010/08/on-removing-java-checked-exceptions-by.html):

   @SuppressWarnings("unchecked")
   private static <T extends Throwable, A> 
     A pervertException(Throwable x) throws T {
      throw (T) x;
   }


   public static <A> A chuck(Throwable t) {
      return Unchecked.
        <RuntimeException, A>pervertException(t);
   }

You might also check out com.google.common.base.Throwables.getRootCause(Throwable) and just print its (root) stack trace.

Upvotes: 2

Kazekage Gaara
Kazekage Gaara

Reputation: 15052

Follow up from my comment. Here's an article that has to throw some light on the issue. It uses sun.misc.Unsafe to rethrow exceptions without wrapping them.

Upvotes: 3

aioobe
aioobe

Reputation: 421280

class IORuntimeException extends RuntimeException {

    final IOException ioex;

    public IORuntimeException(IOException ioex) {
        this.ioex = ioex;
    }

    @Override
    public String getMessage() {
        return ioex.getMessage();
    }

    @Override
    public StackTraceElement[] getStackTrace() {
        return ioex.getStackTrace();
    }

    //@Override
    // ...
}

(Full class available here, as produced by Eclipse "Generate Delegate Methods" macro.)

Usage:

try {
    ...
} catch (IOException ioex) {
    throw new IORuntimeException(ioex);
}

Upvotes: 2

missingfaktor
missingfaktor

Reputation: 92106

Project Lombok allows you to disable checked exceptions altogether.

Upvotes: 4

Related Questions