Scala on Android with scala.concurrent.Future do not report exception on system err/out -
we build android application scala 2.11. use scala.concurrent.future async background tasks. problem is, not see exceptions in logcat if exceptions thrown inside future block.
we create execution context our own reporter:
lazy val reporter: (throwable => unit) = { t => t.printstacktrace() } implicit lazy val exec = executioncontext.fromexecutor( new threadpoolexecutor(10, 100, 5, timeunit.minutes, new linkedblockingqueue[runnable]), reporter)
even if set breakpoint inside reporter debugger never stop here, if force throwing of exceptions insde future {...} block. doing wrong
according comment looks didn't work future needed. when exception occurred during future computation transformed failure case (like failure try, in async context), e.g:
scala> future { 10 / 0 } res21: scala.concurrent.future[int] = scala.concurrent.impl.promise$defaultpromise@24f3ffd
as can see there no exception thrown or printed. handle exception need use callbacks, i.e oncomplete
or onfailure
, e.g:
scala> res21.onfailure { case error => println(s"error: ${ error.getmessage }") } error: / 0
a great intro futures , duality given man psychedelic t-shirt erik meijer in coursers intro reactive programming.
Comments
Post a Comment