Scala: Silently catch all exceptions

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

    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.

提交回复
热议问题