Scala: Silently catch all exceptions

后端 未结 8 1571
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-31 05:34

Empty catch block seems to be invalid in Scala

try {
  func()
} catch {

} // error: illegal start of simple expression

How I can catch all

8条回答
  •  遥遥无期
    2020-12-31 05:57

    What about:

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

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

提交回复
热议问题