NonFatal
scalaの例外処理でよく使われる抽出子。
Javaの例外のうち、アプリで握りつぶしても大丈夫なやつだけを判定してくれる。
code:実装イメージ.scala
object NonFatal {
def apply(t: Throwable): Boolean = t match {
case _: VirtualMachineError | _: ThreadDeath |
_: InterruptedException | _: LinkageError => false
case _ => true
}
def unapply(t: Throwable): OptionThrowable =
if (apply(t)) Some(t) else None // ← fatal でなければ Some、fatal なら None
}
code:使い方.scala
try {
// dangerous stuff
} catch {
case NonFatal(e) => log.error(e, "Something not that bad.")
// or
case e if NonFatal(e) => log.error(e, "Something not that bad.")
}
https://www.scala-lang.org/api/2.13.3/scala/util/control/NonFatal$.html
https://www.scala-lang.org/api/3.3.0/scala/util/control/NonFatal$.html