要将异常转换为Either或Option类型,可以使用中提供的方法scala.util.control.Exception
import scala.util.control.Exception._ val plain = "71a" val optionInt: Option[Int] = catching(classOf[java.lang.NumberFormatException]) opt {plain.toInt} val eitherInt = Either[Throwable, Int] = catching(classOf[java.lang.NumberFormatException]) either {plain.toInt}