Created
March 31, 2015 21:20
-
-
Save bigjason/f1ab20f95cde6a7cdfdd to your computer and use it in GitHub Desktop.
Scala exception `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
| def safely[T](f: PartialFunction[Throwable, T]): PartialFunction[Throwable, T] = new PartialFunction[Throwable, T] { | |
| override def isDefinedAt(x: Throwable): Boolean = NonFatal(x) && f.isDefinedAt(x) | |
| override def apply(v1: Throwable): T = f(v1) | |
| } | |
| try throw new InvalidParameterException("Test") | |
| catch safely { | |
| case exc => exc.printStackTrace() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment