Well, it depends - mostly on what your application is doing, and what possible runtime failures can come up.
Let's categorize runtime exceptions into two buckets: those that we can recover from, and those that we shouldn't recover from.
First, if you have a method that looks like this:
public boolean validateInteger(Integer value) {
return value > 0;
}
...then you'll notice that there's a possible NullPointerException
that could come up as a result of value
being null
. That means that you had bad data coming in and you dealt with it in an inappropriate manner.
Heaven help you if you do this:
Integer value = null;
try {
boolean result = validateInteger(value);
} catch (RuntimeException e) {
e.printStackTrace();
}
What you're doing is ignoring the bad input instead of checking the boundaries. This can lead to a multitude of bugs and logic errors. We shouldn't be continuing on this error.
If, on the other hand, you can recover from some catastrophic "Java can't continue" error, then it'd be safe to do so.
Take a standard formatter used to parse a date, allowing the user to specify their own format.
Formatter formatter = new Formatter();
Date date = new Date();
System.out.println(formatter.format(userProvidedFormat, date);
That could realistically blow up if they mess up the format. Why do we want to stop everything instead of asking the user to provide another format? This is the kind of error we can recover from.
This also adds a bit to the notion of checked exceptions - the API truly believes that the exceptional state you get back is recoverable, so you should be recovering from it.