10

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 ?

5
  • Is this helpful? Commented Jun 8, 2012 at 6:53
  • 2
    If you're irritated by nested stacktraces, you're gonna hate most frameworks and libraries, which tend to use them extensively. I'd just get used to it if I were you. Commented Jun 8, 2012 at 6:58
  • @KazekageGaara, good one. I'd upvote that if you post it as an answer. Commented Jun 8, 2012 at 7:15
  • @artbristol I know, but I can do my part to minimize the problem and avoid 400 line stacktraces. Commented Jun 8, 2012 at 10:18
  • Related: stackoverflow.com/q/39719370/435605 Commented Sep 29, 2016 at 8:26

6 Answers 6

4

Project Lombok allows you to disable checked exceptions altogether.

Sign up to request clarification or add additional context in comments.

2 Comments

Fair enough, but it's too much of a hack to me. I'd rather just do it myself somewhere
I doubt if there is a non-hacky solution to the problem you are trying to solve.
3

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.

Comments

2
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); } 

4 Comments

The problem with this approach is that it cannot be generalized for any Exception type, and thus causes too much boilerplate and code duplication.
Yes, but the type information is lost. Only if Java had reified generics... then we could have done this with generics.
An interesting aspect of this response is that it does not really work ;) "fillInStackTrace" is called from the the throwable constructor, meaning the member variable in the subclass hasnt been initialized.
Ah, so remove @Override ... fillInStackTrace then?
2

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.

Comments

0

sounds like you actually NEED checked Exceptions

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.