如何处理Scala未来中抛出的异常? [英] How do I get hold of exceptions thrown in a Scala Future?

查看:495
本文介绍了如何处理Scala未来中抛出的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在处理我的答案是否有一个标准的Scala函数用于运行块超时?如果在未来抛出异常,就会遇到问题。

  def runWithTimeout [T](timeoutMs:Long) (f:=> T):选项[T] = {
awaitAll(timeoutMs,future(f))head.asInstanceOf [Option [T]]
}

因此

  runWithTimeout 50){result}应该等于(Some(result))
runWithTimeout(50){Thread.sleep(100); result}应该等于(None)

但是如果我在我的块中抛出异常,因此下面的错误会引发..no exception is throw

  intercept [Exception] { 
runWithTimeout(50){throw new Exception(deliberate)}
} .getMessage应该等于(慎重)

Syserr具有包含消息的堆栈跟踪

 < function0& .lang.Exception:deliberate 

但我找不到在打印的Scala运行时的位置。



除了在另一个捕获异常的块中包装f,如果抛出异常并传播它们,有没有办法说服awaitAll和/或Future抛出?

解决方案

简短的答案:不。



例外情况're在线程上下文中工作,因为你想知道调用者中的异常,而异常发生在未来的线程中。



相反,如果你想知道异常是什么,你应该返回一个 Either [Exception,WhatYouWant] - 当然,你必须在未来捕获该异常并打包。 p>

  scala> scala.actors.Futures.future {
try {Right(fail.toInt)} catch {case e:Exception => Left(e)}
}
res0:scala.actors.Future [Product with Serializable with Either [Exception,Int]] =< function0>

scala> res0()//应用未来​​
res1:带有Seri​​alizable的产品带有Either [Exception,Int] =
Left(java.lang.NumberFormatException:对于输入字符串:fail)


I've been working up my answer to Is there a standard Scala function for running a block with a timeout?, and have run into a problem if an exception is thrown in a Future.

  def runWithTimeout[T](timeoutMs: Long)(f: => T) : Option[T] = {
    awaitAll(timeoutMs, future(f)).head.asInstanceOf[Option[T]]
  }

So that

runWithTimeout(50) { "result" } should equal (Some("result"))
runWithTimeout(50) { Thread.sleep(100); "result" } should equal (None)

But if I throw an exception in my block it doesn't leak, but is swallowed - so that the following fails with "..no exception was thrown"

intercept[Exception] {
    runWithTimeout(50) { throw new Exception("deliberate") }
}.getMessage should equal("deliberate")

Syserr has a stack trace with the message

<function0>: caught java.lang.Exception: deliberate

but I can't find where in the Scala runtime that is printed.

Apart from wrapping f in another block which catches exceptions and propagates them if thrown, is there any way to persuade awaitAll and/or Future to throw?

解决方案

Short answer: no.

Exceptions don't do what you want when you're working in a threaded context, because you want to know about the exception in the caller, and the exception happens in the future's thread.

Instead, if you want to know what the exception was, you should return an Either[Exception,WhatYouWant]--of course, you have to catch that exception within the future and package it up.

scala> scala.actors.Futures.future{
  try { Right("fail".toInt) } catch { case e: Exception => Left(e) }
}
res0: scala.actors.Future[Product with Serializable with Either[Exception,Int]] = <function0>

scala> res0()   // Apply the future
res1: Product with Serializable with Either[Exception,Int] =
      Left(java.lang.NumberFormatException: For input string: "fail")

这篇关于如何处理Scala未来中抛出的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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