3 ms·
Minor nitpick: OutOfMemoryError and StackOverflowError are subclasses of Error, not Exception. It is an anti-pattern to catch any Error subclasses since these a
by koito17 2y ago
Minor nitpick: OutOfMemoryError and StackOverflowError are subclasses of Error, not Exception. It is an anti-pattern to catch any Error subclasses since these are usually JVM-internal exceptions. Programs are firmly in undefined behavior territory when they e.g. catch an OutOfMemoryError and do nothing with it. This is why catching Throwable is considered a bad idea compared to Exception. In the majority of cases, there is no reasonable recovery from a thrown Error, unlike a thrown Exception.
With that said, it is true RuntimeException and its subclasses are unchecked exceptions. Thankfully, those exceptions tend to be things like IllegalArgumentException, NullPointerException, etc., which are still pretty serious but not as bad as the two examples given.
- MBCook 2y agoAh that’s right. I’m used to RuntimeException since that’s what people subclass to get out of the checked exception mess. The exact hierarchy around rare things like OutOfMemory just isn’t something that ever comes up in day to day work so it’s easy to forget. Thanks for the correction.
- dwaite 2y agoThe ThreadDeath Error in particular is interesting because it is catchable but not "handle-able" - if you do not rethrow ThreadDeath in your exception handler, the JVM will do so for you. Despite Thread.stop() being deprecated, it is still used in things like application servers to deal with user code which is not completing in a specified timeframe.