Last active
June 28, 2018 17:59
-
-
Save frgomes/89cc1ef2e816f49c4425 to your computer and use it in GitHub Desktop.
Scala - Handling Throwables safely
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Safely handles ControlThrowable | |
* | |
* See: [[https://www.sumologic.com/2014/05/05/why-you-should-never-catch-throwable-in-scala/ Why you should never catch Throwable in Scala ]] | |
* | |
* See: [[https://github.com/scala/scala/blob/2.12.x/src/library/scala/util/control/NonFatal.scala scala.util.control.NonFatal.scala] | |
*/ | |
trait CatchSafely { | |
import scala.util.control.ControlThrowable | |
def safely[T](handler: PartialFunction[Throwable, T]): PartialFunction[Throwable, T] = { | |
// if it is a condition which cannot be handled by application code... | |
case t: VirtualMachineError => throw t | |
case t: ThreadDeath => throw t | |
case t: InterruptedException => throw t | |
case t: LinkageError => throw t | |
case t: ControlThrowable => throw t | |
// if it's a Throwable the caller handles, pass it on to the caller... | |
case t: Throwable if handler.isDefinedAt(t) => handler(t) | |
// if it's a Throwable not handled by the caller, just rethrow. | |
case t: Throwable => throw t | |
} | |
} | |
object CatchSafely extends CatchSafely |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another alternative, which is provided as part of Scala runtime library is
NonFatal
, as shown below: