本文介绍了Scala 的“尝试"是什么?意味着没有 catch 或 finally 块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与 Java 不同,Scala 允许您进行简单的尝试",没有 catch 或 finally 子句:

Unlike Java, Scala lets you do a bare "try", without either a catch or finally clause:

scala> try { println("Foo") }
Foo

这实际上还有什么意义吗,

Does this actually have any meaning beyond,

{ println("Foo") }

?

推荐答案

Scala 的异常处理通过将任何异常传递给匿名捕获函数来工作.catch 函数通过模式匹配捕获的异常来工作,如果不匹配,它将向上传递异常.

Scala's exception handling works by passing any exceptions to an anonymous catch function. The catch function works by pattern matching the caught exception and, if it doesn't match it will pass the exception up.

catch 函数是可选的,如果它被省略,则异常直接传递.所以本质上

The catch function is optional, if it's omitted then the exception is passed straight up. So essentially

try { exceptionThrowingFunction() }

exceptionThrowingFunction()

请参阅语言规范的第 6.22 章 pdf 了解更多信息.

See chapter 6.22 of the language spec pdf for more information.

这篇关于Scala 的“尝试"是什么?意味着没有 catch 或 finally 块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 19:20