Skip to content

Instantly share code, notes, and snippets.

@johntbush
Last active April 26, 2016 21:42
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/f00b169788da1a6fa1fea0d59b165cf7 to your computer and use it in GitHub Desktop.
Save johntbush/f00b169788da1a6fa1fea0d59b165cf7 to your computer and use it in GitHub Desktop.
scala> import scala.concurrent.Future
import scala.concurrent.Future
scala> import scala.util.{Failure, Success, Try}
import scala.util.{Failure, Success, Try}
scala> import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.Implicits.global
scala>
scala> def fail = throw new RuntimeException("error now")
fail: Nothing
scala>
scala> def failInFuture() = Future { throw new RuntimeException("error in future") }
failInFuture: ()scala.concurrent.Future[Nothing]
scala>
scala> try {
| fail
| } catch {
| case e: Exception => println(e.getMessage)
| }
error now
scala>
scala> failInFuture().onComplete {
| case Success(result) =>
| case Failure(t) => println("error reported in onComplete")
| }
error reported in onComplete
scala>
scala> failInFuture().onFailure {
| case e => println("error reported in onFailure")
| }
error reported in onFailure
scala>
scala> failInFuture().recover {
| case e => println("error reported in recover")
| }
res24: scala.concurrent.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@bd1111a
error reported in recover
scala>
scala>
scala> Try (fail).failed.map(e => println(e.getMessage))
error now
res25: scala.util.Try[Unit] = Success(())
scala> failInFuture().failed.map(e => println(e.getMessage))
error in future
res26: scala.concurrent.Future[Unit] = scala.concurrent.impl.Promise$DefaultPromise@65753040
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment