Scala: Silently catch all exceptions

后端 未结 8 1585
佛祖请我去吃肉
佛祖请我去吃肉 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 06:01

    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(func()) match {
      case Success(answer) => continue(answer)
      case Failure(e) => //do nothing
    }
    

提交回复
热议问题