Why all Java Errors are not considered fatal in Scala?

Viewed 101

In the Scala 2.13 docs, the description for NonFatal is:

Extractor of non-fatal Throwables. Will not match fatal errors like VirtualMachineError (for example, OutOfMemoryError and StackOverflowError, subclasses of VirtualMachineError), ThreadDeath, LinkageError, InterruptedException, ControlThrowable.

Why are only these errors / exceptions considered fatal? In Java8, there are more Error subclasses, each of which "indicates serious problems that a reasonable application should not try to catch". Example of these include CoderMalfunctionError, FactoryConfigurationError, and IOError. I won't claim to know the precise meanings of these errors, but if Java thinks these errors are serious problems, why shouldn't Scala also think the same?

1 Answers

SethTisue asked a similar quesiton

@viktorklang tbh I don't really understand why any Error would be considered NonFatal. the JVM already has the Error vs non-Error distinction, I've never been especially clear on why NonFatal adds an additional level of classification.

and viktorklang replied

@SethTisue «An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. » - https://docs.oracle.com/javase/8/docs/api/java/lang/Error.html

The problem is that there are quite a few unreasonable applications out there, and since Error is extendable it is impossible to say that all Errors are fatal.

Hence it seems that because there are sufficient number of applications in the wild which violate the intended semantics of Error, then Scala contributors were hesitant to define them all as fatal.

Also consider gitter discussion around the following comment

Edmund Noble @edmundnoble Feb 22 2018 19:39

Afaik Error is supposed to be fatal Though as @Ichoran says there are exceptions, I just don't think they're well-defined, and you basically need to whitelist the errors you think aren't fatal

Related