Try/Catch:Object Exception不是值

And*_*rew 2 scala

我试图捕获不同类型的异常,但我陷入了错误.我进口了scala.util.control.Exception._

try {
        isAuthenticatedJson(f)
    }catch {
      //object RuntimeException is not a value
      case RuntimeException => {}
      //object Exception is not a value
      case Exception => {}
    }  
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Rég*_*les 11

试试这个(注意下划线):

try {
    isAuthenticatedJson(f)
} catch {
  case _: RuntimeException => {}
  case _: Exception => {}
}
Run Code Online (Sandbox Code Playgroud)

这是一种"打字模式".见http://www.scala-lang.org/node/120.