Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging of exceptions #195

Open
aschweig opened this issue Dec 3, 2019 · 0 comments
Open

Logging of exceptions #195

aschweig opened this issue Dec 3, 2019 · 0 comments

Comments

@aschweig
Copy link

aschweig commented Dec 3, 2019

I was hunting for idiomatic approaches to logging exceptions with scala-logging. I was impressed with the Try approach described at the end of this answer.

object ExceptionExample extends LazyLogging {
  def main(args:Array[String]):Unit = {
    val maybeResult = Try[Int] { "aa".toInt } match {
      case failure @ Failure(throwable) =>
        throwable match {
          case _:ArithmeticException => Success(-1) // Recover from e.g., 1/0
          case runtimeException: RuntimeException =>  // Log RuntimeExceptions
            logger.error("ExceptionMsg", runtimeException); failure
        }
      case success @ _ => success
    }
    println("Getting result");
    val result = maybeResult.get // Will Rethrow on Failure
    println("result = " + result.toString);
  }
}

Is there an approach with less boilerplate?

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

No branches or pull requests

1 participant