Skip to content

Instantly share code, notes, and snippets.

@bigjason
Created March 31, 2015 21:20
Show Gist options
  • Select an option

  • Save bigjason/f1ab20f95cde6a7cdfdd to your computer and use it in GitHub Desktop.

Select an option

Save bigjason/f1ab20f95cde6a7cdfdd to your computer and use it in GitHub Desktop.
Scala exception `safely`
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