object NonFatal {
/**
* Returns true if the provided `Throwable` is to be considered non-fatal, or false if it is to be considered fatal
* /
def apply(t: Throwable): Boolean = t match {
// VirtualMachineError includes OutOfMemoryError and other fatal errors
case _: VirtualMachineError | _: ThreadDeath | _: InterruptedException | _: LinkageError | _: ControlThrowable => false
case _ => true
}
/**
* Returns Some(t) if NonFatal(t) == true, otherwise None
*/
def unapply(t: Throwable): Option[Throwable] = if (apply(t)) Some(t) else None
}
这意味着抛出的每个异常(除了致命的异常)都将在该案例中被捕获
case NonFatal(e) => println("I want to get to this point")
1条答案
按热度按时间kyks70gy1#
nonfatal是定义非致命错误的scala对象。这里是定义
这意味着抛出的每个异常(除了致命的异常)都将在该案例中被捕获
因为在不适用的情况下,非致命异常是一些(t),而致命异常是无。看到了吗https://docs.scala-lang.org/tour/extractor-objects.html 供参考。
您可以抛出任何非致命异常。