Is there a better way to throw this exception?
Weijun Wang
weijun.wang at oracle.com
Thu Jun 6 02:42:53 PDT 2013
Hi All
I have a method that could throw two kinds of checked exceptions and
possibly other unchecked ones:
void once() throws One, Two
Now I have a wrapper method that calls once() for multiple times, and
want to throw the first exception if *all* fails. Now it looks like
void multiple() throws One, Two {
Exception saved = null;
for (all chances) {
try {
once();
return;
} catch (Exception e) {
if (saved != null) saved = e;
}
}
if (saved instanceof One) {
throw (One)saved;
} else if (saved instanceof One) {
throw (Two)saved;
} else if (saved instanceof RuntimeException) {
throw (RuntimeException)saved;
} else {
// Not likely, but I've already wrote so many lines.
throw new RuntimeException(saved);
}
}
Is there any way I can make it shorter?
Thanks
Max
More information about the compiler-dev
mailing list