斯卡拉在Android上scala.concurrent.Future不要在系统错误输入/输出异常报告

编程入门 行业动态 更新时间:2024-10-22 18:37:20
本文介绍了斯卡拉在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不要在系统错误输入/输出异常报告

本文发布于:2023-11-25 13:57:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斯卡   异常   错误   报告   系统

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!