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

标签 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`?
  }
}

如何确定Failure包含的异常类型?我需要根据异常类型采取不同的措施。

最佳答案

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

或者
case Success(f) =>
case Failure(e) => e match {
   case e1: ExceptionType1 =>
   case e2: ExceptioNType2 =>
   case _ => 
}

关于Scala:如何确定故障的异常类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20940558/

相关文章:

scala - Slick 3.1.1 模糊隐式值涉及过滤器和隐式 CanBeQueryCondition

android - 哪些语言在Android中开发原生应用比较成熟[Java除外]

c# - 如何在使用 try catch 后更好地调试

exception - Elasticsearch 查询 "empty index"

android-intent - Google Smart Home响应模式不适用于authFailure错误代码

python - 添加矩阵python

Javascript 在 catch block 中丢失了回溯

regex - 使用 scala future 的大文件的多个并行 grep 没有所需的速度

scala - 我需要 sbt 0.11.2 来构建用于 lift 的 mongo auth 应用程序

python - 如何在 Python 中打印异常对象的堆栈跟踪?