scala - Exceptions Thrown by Await#result -
given following code:
import spray.http._ import spray.client.pipelining._ import scala.concurrent.future implicit val system = actorsystem() import system.dispatcher // execution context futures val pipeline: httprequest => future[httpresponse] = sendreceive val response: future[httpresponse] = pipeline(get("http://spray.io/"))
the following pseudo-code function waits 10 seconds, returning "good" if httpresponse
returned, or "bad" on await#result
exception (see docs.
import scala.concurrent.await import scala.concurrent.duration._ def f(fut: future[httpresponse]): string = { try { val result = await.result(fut, 10.seconds) "good" } catch e @ (_: interruptedexception | _: illegalargumentexception | _: timeoutexception ) => "bad" }
in catch
, necessary catch exception thrown await#result
? in other words, not catching possible exceptions here?
the await.result
can throw exceptions caught, however, if future awaits not complete successfully, forwards exception contained future. might want read blocking section here: futures , promises.
so yes, there may exceptions aren't catching, can result failed computation of httpresponse
.
blocking in real code bad , should done testing purposes, if need to, recommend wrap await in scala.util.try
manipulate elegantly later , keep information of when , why failed.
Comments
Post a Comment