scala.util.Try:如何获得 Throwable 值?模式匹配?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25588387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
scala.util.Try: How to get Throwable value? Pattern matching?
提问by clay
The following REPL snippets presume:
以下 REPL 片段假定:
import scala.util.{Try, Success, Failure}
Why do these two statements not pass compilation? I get "constructor cannot be instantiated to expected type":
为什么这两个语句编译不通过?我得到“构造函数无法实例化为预期类型”:
Failure(new Exception("a")) match {
case Success(i) => "a"; case Failure(e) => "b"; case _ => "c"; }
Success(123) match {
case Success(i) => "a"; case Failure(e) => "b"; case _ => "c"; }
I can get the success value out of a Trywith getor toOption. Is there a corresponding way to get the failing Throwablevalue or Option[Throwable]?
我可以从Trywithget或 中获得成功值toOption。是否有相应的方法来获取失败的Throwable值或Option[Throwable]?
EDIT: Casting from Failure/Success to Try works
编辑:从失败/成功转换到尝试工作
Failure(new Exception("a")).asInstanceOf[Try[Int]] match {
case Success(i) => "a"; case Failure(e) => "b"; case _ => "c"; }
Success(123).asInstanceOf[Try[Int]] match {
case Success(i) => "a"; case Failure(e) => "b"; case _ => "c"; }
回答by Michael Zajac
Consider this:
考虑一下:
Try(1) match {
case Success(i) => i
case Failure(t) => 0 // t is the `Throwable`
}
This works because Successand Failureare sub classes of the abstract class Try. However, the following code fails to compile, because you're no longer matching on a generic Try, and instead a Failurewhich can neverbe an instance of Success.
这是有效的,因为Success和Failure是抽象类的子类Try。但是,下面的代码编译失败,因为你是在一个通用的不再匹配Try,而是一个Failure可以永远是一个实例Success。
Failure(new Exception("a")) match {
case Success(i) => "a" // You can see it compiles if you remove this line.
case Failure(e) => "b"
case _ => "c"
}
This is like trying to match an Integerto a String, it doesn't really make sense.
这就像试图将 anInteger与 a匹配String,它实际上没有意义。
If you want to get the Throwablevia pattern matching, see the first snippet of code.
如果您想获得Throwablevia 模式匹配,请参阅第一个代码片段。
Another way you could extract the Throwablewould be to use the failedmethod on your Try, which will wrap the Throwablefrom a failure within Success.
您可以提取 的另一种方法是在您Throwable的failed上使用方法Try,它将Throwable从失败中包装Success。
scala> val t: Throwable = Try(throw new Exception).failed.get
t: Throwable = java.lang.Exception
Calling this on a Success, however, will throw another exception.
Success但是,在 a 上调用它会引发另一个异常。
回答by Chris Martin
The first snippet doesn't compile because Successisn't a subtype of Failure. The compiler thinks you're being silly, since the Success(i)case will never match.
第一个片段无法编译,因为Success它不是Failure. 编译器认为你很傻,因为这种Success(i)情况永远不会匹配。
To take a simpler example, this also doesn't compile.
举一个更简单的例子,这也不能编译。
Failure(new Exception()) match { case Success(_) => }
Neither does this, for the same reason.
出于同样的原因,这也不行。
42 match { case Success(_) => }
What you've written does nearly work, but the value you're matching has to have a more general type of Try[_](which it would have anyway, if the pattern match were actually being used in some useful context).
您编写的内容几乎可以工作,但是您匹配的值必须具有更通用的类型Try[_](如果模式匹配实际上在某些有用的上下文中使用,它无论如何都会具有)。
(Failure(new Exception("a")): Try[_]) match {
case Success(i) => "a"; case Failure(e) => "b"; case _ => "c"; }

