Skip to content

Instantly share code, notes, and snippets.

@johntbush
Last active April 26, 2016 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johntbush/eed56819180de112cbca107d00e5b3e5 to your computer and use it in GitHub Desktop.
Save johntbush/eed56819180de112cbca107d00e5b3e5 to your computer and use it in GitHub Desktop.
import scala.concurrent.Future
import scala.util.{Failure, Success, Try}
import scala.concurrent.ExecutionContext.Implicits.global
def fail = throw new RuntimeException("error now")
def failInFuture() = Future { throw new RuntimeException("error in future") }
try {
fail
} catch {
case e: Exception => println(e.getMessage)
}
failInFuture().onComplete {
case Success(result) =>
case Failure(t) => println("error reported in onComplete")
}
failInFuture().onFailure {
case e => println("error reported in onFailure")
}
failInFuture().recover {
case e => println("error reported in recover")
}
Try (fail).failed.map(e => println(e.getMessage))
failInFuture().failed.map(e => println(e.getMessage))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment