I want to handle different cases of Failure (returned as Try).
example code
main(args(0)) match {
case Success(result) => result.foreach(println)
case Failure(ex) => ex match {
case FileNotFoundException =>
System.err.println(ex.getMessage)
case StatsException =>
System.err.println(ex.getMessage)
case _ => {
ex.printStackTrace()
}
System.exit(1)
}
}
If its a StatsException
or FileNotFoundException
just print the message, for all other exceptions print a stack trace.
However ex is only ever a Throwable, and so case StatsException
is a fruitless type test (a value of type Throwable cannot also be a StatsException.type according to IntelliJ)
Worse I get compile errors: java.io.FileNotFoundException is not a value
What's the best way to handle different cases of Failure in an idiomatic way?