SRobertJames SRobertJames - 1 year ago 108
Scala Question

Scala - Try with exception logging


is very useful. I'd like to use that pattern, but log all exceptions. How can I do this?

Answer Source

Define the following helper:

import scala.util.{Try, Failure}

def LogTry[A](computation: => A): Try[A] = {
  Try(computation) recoverWith {
    case e: Throwable =>
      new Failure(e)

Then you can use it as you would use Try, but any exception will be logged through log(e).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download