为什么在Scala中不认为所有的Java错误都是致命的?

huangapple go评论72阅读模式
英文:

Why all Java Errors are not considered fatal in Scala?

问题

Scala 2.13文档中,关于NonFatal的描述如下:

> 用于提取非致命的可抛出异常。不会匹配致命错误,比如VirtualMachineError(例如,OutOfMemoryError和StackOverflowError,VirtualMachineError的子类),ThreadDeath,LinkageError,InterruptedException,ControlThrowable。

为什么只有这些错误/异常被认为是致命的?在Java8中,有更多的Error子类,每个都表示“合理的应用程序不应该尝试捕获的严重问题”。其中包括CoderMalfunctionErrorFactoryConfigurationErrorIOError。我不会声称知道这些错误的确切含义,但如果Java认为这些错误是严重问题,为什么Scala不应该也这样认为呢?

英文:

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

得分: 3

SethTisue提出了类似的问题

> @viktorklang to be honest, 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.

viktorklang回答道

> @SethTisue "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.

因此,似乎由于野外存在足够多违反 Error 预期语义的应用程序,Scala 的贡献者们对将它们全部定义为致命的感到犹豫不决。

还考虑一下关于以下评论的 gitter 讨论

> Edmund Noble @edmundnoble 于 2018 年 2 月 22 日 19:39 说
>
> 据我所知,Error 应该是致命的。虽然正如 @Ichoran 所说,也有例外情况,但我认为它们不太明确,基本上你需要列出你认为不是致命的错误。

英文:

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

huangapple
  • 本文由 发表于 2020年7月21日 22:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63016426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定