Scala: Silently catch all exceptions

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

    Try adding

    case x =>
    

    in your catch block :)

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题