Scala:默默地捕捉所有异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21170322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:59:48  来源:igfitidea点击:

Scala: Silently catch all exceptions

scalaexceptionfunctional-programmingtry-catch

提问by tmporaries

Empty catch block seems to be invalid in Scala

空的 catch 块在 Scala 中似乎无效

try {
  func()
} catch {

} // error: illegal start of simple expression

How I can catch all exceptions without their processing?

我如何在没有处理的情况下捕获所有异常?

回答by Rex Kerr

Some exceptions really aren't meant to be caught. You can request it anyway:

有些异常真的不应该被捕获。您无论如何都可以请求它:

try { f(x) }
catch { case _: Throwable => }

but that's maybe not so safe.

但这可能不太安全。

All the safe exceptions are matched by scala.util.control.NonFatal, so you can:

所有安全异常都由 匹配scala.util.control.NonFatal,因此您可以:

import scala.util.control.NonFatal
try { f(x) }
catch { case NonFatal(t) => }

for a slightly less risky but still very useful catch.

对于风险稍低但仍然非常有用的捕获。

Or scala.util.Trycan do it for you:

或者scala.util.Try可以为您做:

Try { f(x) }

and you can pattern match on the result if you want to know what happened. (Trydoesn't work so well when you want a finallyblock, however.)

如果你想知道发生了什么,你可以对结果进行模式匹配。(但是,Try当您想要一个finally块时,效果不佳。)

回答by elm

In

import scala.util.Try

val res = Try (func()) toOption 

if the Try is successful you will get a Some(value), if it fails a None.

如果 Try 成功,您将获得Some(value),如果失败则为None

回答by Akos Krivachy

Inside of Scala's catch block you need to use similar construct as in a match statement:

在 Scala 的 catch 块中,您需要使用与 match 语句中类似的构造:

try {
  func()
} catch {
  case _: Throwable => // Catching all exceptions and not doing anything with them
}

回答by Tamere

Try adding

尝试添加

case x =>

in your catch block :)

在您的 catch 块中 :)

回答by som-snytt

If annotating with Throwable is burdensome, you can also

如果用 Throwable 注释很麻烦,你也可以

scala> def quietly[A]: PartialFunction[Throwable, A] = { case _ => null.asInstanceOf[A] }
quietly: [A]=> PartialFunction[Throwable,A]

scala> val x: String = try { ??? } catch quietly
x: String = null

which has the small virtual of being self-documenting.

它具有自我记录的小虚拟。

scala> val y = try { throw new NullPointerException ; () } catch quietly
y: Unit = ()

回答by om-nom-nom

Just for a sake of completeness, there is a set of built-in methods in Exception, including silently catching exceptions. See Using scala.util.control.Exceptionfor details on usage.

为了完整起见,Exception 中有一组内置方法,包括静默捕获异常。有关用法的详细信息,请参阅使用 scala.util.control.Exception

回答by Johnny

I like one of the suggestions here to use Try()with toOption, but in case you don't want to go further with Option, you can just do:

我喜欢这里使用Try()with的建议之一toOption,但如果您不想进一步使用 Option,您可以这样做:

Try(func()) match {
  case Success(answer) => continue(answer)
  case Failure(e) => //do nothing
}

回答by leo9r

What about:

关于什么:

import scala.util.control.Exception.catching
catching(classOf[Any]) opt {func()}

It generates an Option like in some of the previous answers.

它会生成一个 Option 就像之前的一些答案一样。