Consider this:
Try(1) match { case Success(i) => i case Failure(t) => 0
This works because Success and Failure are subclasses of the abstract Try class. However, the following code does not compile because you are no longer mapped to a common Try , but instead Failure , which can never be an instance of Success .
Failure(new Exception("a")) match { case Success(i) => "a"
This is like trying to map Integer to String , it really doesn't make sense.
If you want to get Throwable through a template, see the first code snippet.
Another way you could extract Throwable would be to use the failed method on your Try , which will wrap Throwable from a failure in Success .
scala> val t: Throwable = Try(throw new Exception).failed.get t: Throwable = java.lang.Exception
Calling this in Success , however, will raise another exception.
Michael zajac
source share