Skip to content

Instantly share code, notes, and snippets.

@jseteny
Last active June 19, 2017 10:29
Show Gist options
  • Select an option

  • Save jseteny/b9dfbbe1217e5d4d28c482fe51afe7e1 to your computer and use it in GitHub Desktop.

Select an option

Save jseteny/b9dfbbe1217e5d4d28c482fe51afe7e1 to your computer and use it in GitHub Desktop.
To use the root cause of an exception conveniently:
implicit class ThrowableWithRootCause(throwable: Throwable) {
def rootCause: Throwable = cause(throwable)
private def cause(t: Throwable): Throwable = t.getCause match {
case null => t
case _ => cause(t.getCause)
}
}
@jseteny
Copy link
Author

jseteny commented Jun 19, 2017

Usage:

try {
throw new RuntimeException("Panic")

} catch {
case t: Throwable =>
System.err.println("An error has occured: ")
t.rootCause.printStackTrace(System.err)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment