Thursday, May 17, 2007

The cause of an IOException

I just had to follow a stack trace through a complex codebase with multiple layers. The exception chaining mechanism introduced in Java 1.4 made the task easy up to the point where the last exception in the chain was an IOException thrown by code like this:

try {
....
} catch (Exception e) {
throw new IOException("...");
}


What a dead end! The problem is that the IOException constructors that allow exception chaining were only added in Java 6. Here's a workaround that would have saved me a lot of extra effort:

try {
....
} catch (Exception e) {
IOException ioe = new IOException("...");
ioe.initCause(e);
throw ioe;
}

1 comment:

  1. Java exceptions are a PITA to debug, this bug[1] "Exception messages are often uninformative" has been open since 2001...

    [1] http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4404007

    ReplyDelete