Optional exception catching
Is it possible to make an exception that is optional to be caught?
In other words, an exception that can either:
be caught in a try-catch block
or skipped if no try-catch block exists for it
To visualize, I have a ReportedException, which is just a plain subclass
of RuntimeException, and want to be able to catch it when it's needed:
try
{
hideWindow();
}
catch (ReportedException ex)
{
// Window could not be hidden.
// Exception has already been caught and logged by parseInput(),
// and now we're going to do something more.
if (!(ex.getCause() instanceof NullPointerException))
{
killWindow();
}
}
or skip catching if the result is irrelevant:
hideWindow(); // We don't care if it's been hidden or not.
openAnotherWindow();
I know I can leave the catch block empty and have the same thing as above,
but I use ReportedException very often and it would make my code highly
unreadable. I also want to be able to get the exception cause.
If it's impossible (I suspect it is), what alternative/walkaround would
you recommend?
P.S. The method names used in the examples are just foo's and bar's.
No comments:
Post a Comment