从Scala中的异常返回的正确方法是什么?

Chr*_*ler 7 scala

在非功能语言中,我可能会这样做:

try {
  // some stuff
} catch Exception ex {
  return false;
}

// Do more stuff

return true;
Run Code Online (Sandbox Code Playgroud)

但是,在Scala中,这种模式显然不正确.如果我的scala代码如下所示:

try {
  // do some stuff
}
catch {
  case e: Exception => // I want to get out of here and return false
  )
}

// do more stuff

true
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?当然,我不想使用"返回"语句,但我也不想通过"做更多的东西"并最终返回true.

Gab*_*lla 11

您希望表示可以成功或发出错误信号的计算.这是Trymonad 的完美用例.

import scala.util.{ Try, Success, Failure }

def myMethod: Try[Something] = Try {
  // do stuff
  // do more stuff
  // if any exception occurs here, it gets wrapped into a Failure(e)
}
Run Code Online (Sandbox Code Playgroud)

因此,您将返回一个Try而不是一个Bool,这是一个无限更清晰和惯用.

用法示例:

myMethod match {
  case Success(x) => println(s"computation succeded with result $x")
  case Failure(e) => println(s"computation failed with exception $e.getMessage")  
}
Run Code Online (Sandbox Code Playgroud)

如果您甚至不关心异常,但只是想在成功的情况下返回值,您甚至可以将其转换TryOption.

def myMethod: Option[Something] = Try {
  // do stuff
  // do more stuff
  // return something
  // if any exception occurs here, it gets wrapped into a Failure(e)
}.toOption

myMethod match {
  case Some(x) => println(s"computation succeded with result $x")
  case None => println("computation failed")  
}
Run Code Online (Sandbox Code Playgroud)

要回复评论中的问题,您可以这样做

Try {
  // do stuff
} match {
   case Failure(_) => false
   case Success(_) =>
     // do more stuff
     // true
}
Run Code Online (Sandbox Code Playgroud)

虽然我建议在有意义的Boolean时候返回比a更有意义的东西.

当然这可以嵌套

Try {
  // do stuff
} match {
   case Failure(_) => false
   case Success(_) =>
     // do more stuff
     Try {
       // something that can throw
     } match {
       case Failure(_) => false
       case Success(_) =>
         // do more stuff
         true
     }
}
Run Code Online (Sandbox Code Playgroud)

但你应该考虑将这些Try块放入单独的函数中(返回a Try).

最终,我们可以利用Trymonad 的事实,并做这样的事情

Try { /* java code */ }.flatMap { _ =>
  // do more stuff
  Try { /* java code */ }.flatMap { _ =>
    // do more stuff
    Try { /* java code */ }
  }
} match {
  case Failure(_) => false // in case any of the Try blocks has thrown an Exception 
  case Success(_) => true // everything went smooth
}
Run Code Online (Sandbox Code Playgroud)