斯卡拉在Android上scala.concurrent.Future不要在系统错误输入/输出异常报告 [英] Scala on Android with scala.concurrent.Future do not report exception on system err/out

查看:205
本文介绍了斯卡拉在Android上scala.concurrent.Future不要在系统错误输入/输出异常报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们建立与斯卡拉2.11 Android应用程序。我们使用scala.concurrent.Future为异步后台任务。问题是,我们没有看到logcat的任何异常,如果异常的Future块中抛出。

We build an android application with Scala 2.11. We use scala.concurrent.Future for async background tasks. The problem is, that we do not see any exceptions in logcat if exceptions are thrown inside a Future block.

我们已经用我们自己的记者建立一个执行上下文:

We already create an execution context with 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)

即使我设定的记者中的断点的调试器不会停止在这里,就算我强迫异常投掷insde一个未来的{...}块。
什么是我们做错了

Even if I set a breakpoint inside the reporter the debugger does never stop here, even if I force throwing of exceptions insde a Future {...} block. What are we doing wrong

推荐答案

根据您的评论它看起来就像你根本不与未来需要工作。当未来的计算过程中发生了一些异常就会变身成为失败的情况下(比如从尝试失败,但是在异步上下文),例如:

According to your comment it looks like you simply didn't work with Future as needed. When some exception occurred during the Future computation it is transformed into Failure case (like Failure from Try, but in async context), e.g:

scala> Future { 10 / 0 }
res21: scala.concurrent.Future[Int] = scala.concurrent.impl.Promise$DefaultPromise@24f3ffd

正如你所看到的没有什么异常抛出或打印。为了处理这个异常,你需要使用回调,即的onComplete onFailure处,例如:

scala> res21.onFailure { case error => println(s"Error: ${ error.getMessage }") }
Error: / by zero

一个伟大的介绍到期货和偶被人用迷幻的T恤埃里克·梅耶尔在coursers给出介绍到无编程

A great intro into Futures and Duality was given by the man with psychedelic T-Shirt Erik Meijer in the coursers intro to Reactive Programming.

这篇关于斯卡拉在Android上scala.concurrent.Future不要在系统错误输入/输出异常报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆