Java Reference
In-Depth Information
parameterless] constructor, including a checked exception. Use of this method effectively bypasses
the compile-time exception checking that would otherwise be performed." Once you know this, it's
not too hard to write a sneakyThrow equivalent:
// Don't do this either - circumvents exception checking!
public class Thrower {
private static Throwable t;
private Thrower() throws Throwable {
throw t;
}
public static synchronized void sneakyThrow(Throwable t) {
Thrower.t = t;
try {
Thrower.class.newInstance();
} catch (InstantiationException e) {
throw new IllegalArgumentException();
} catch (IllegalAccessException e) {
throw new IllegalArgumentException();
} finally {
Thrower.t = null; // Avoid memory leak
}
}
}
A few subtle things are going on in this solution. The exception to be thrown during constructor
execution can't be passed to the constructor as a parameter, because Class.newInstance invokes a
 
 
Search WWH ::




Custom Search