Scala:如何确定故障的异常类型

Scala:如何确定故障的异常类型,scala,exception,error-handling,Scala,Exception,Error Handling,请看以下代码片段: userService.insert(user) match { case Success(f) => Logger.debug("User created successfully") case Failure(e) => { // how do I determine the type of `e`? } } 如何确定故障所包含的异常类型?我需要根据异常类型采取不同的操作 case Success(f) => case Fail

请看以下代码片段:

userService.insert(user) match {
  case Success(f) => Logger.debug("User created successfully")
  case Failure(e) => {
     // how do I determine the type of `e`?
  }
}
如何确定故障所包含的异常类型?我需要根据异常类型采取不同的操作

case Success(f) => 
case Failure(e: ExceptionType1) =>
case Failure(e: ExceptionType2) => 
case Failure(e) => // other


@迪迪埃,你觉得哪个更地道?后者对我来说似乎更清楚,因为内部匹配只与
异常相关。不确定其中是否有一个更标准。我相信我会使用第一个,除非在失败的情况下有一个常见的行为:您可以在失败的情况下使用e匹配以外的其他指令。在我看来,两者都很好,用哪一种适合你。我认为第一种更清晰,因为它避免了筑巢;即使在存在共享行为的情况下,我也更倾向于使用
案例失败(e:Ex1)|失败(e:Ex2)=>…
。可能存在一些异常共同的行为,也可能存在一些特定的行为?
case Success(f) =>
case Failure(e) => e match {
   case e1: ExceptionType1 =>
   case e2: ExceptioNType2 =>
   case _ => 
}