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

标签 java scala error-handling

Scala 2.13 docsNonFatal 的描述是:

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

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

最佳答案

SethTisue问了一个类似的问题

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

viktorklang回复了

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

因此,似乎因为在野外有足够多的应用程序违反了 Error 的预期语义,所以 Scala 贡献者不愿将它们全部定义为致命的。

还要考虑围绕以下 comment 进行的 gitter 讨论

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

关于java - 为什么所有 Java 错误在 Scala 中都不被认为是致命的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63016426/

相关文章:

ruby-on-rails - 如何在运行 bundle exec foreman start 时摆脱 Permission denied (publickey) 错误?

scala - IntelliJ 不断从 akka 教程中删除 context.dispatcher 的导入

javascript - 如何找出创建类实例时传递的参数数量?

java - 如何在 spring security 中取消 cookie?

java - 如何在 Gradle 项目中使用 Java 语言进行编码并确保代码在 Android 上运行

scala - Cats Future Monad 给出运行时异常

scala - Play 是否为 queryString 值顺序提供任何保证?

android对话框问题

java - 使用 jitWatch 编译错误

java - Java中HBase表的自定义过滤