When can the future return an exception that is not thrown from the body of the future?

A more general question is: in the product quality code, is it necessary to relate to the exceptions created by ExecutionContext or other concurrency infrastructure outside the execution of the future body? For example, if any failure occurred with the thread pool, will I see an exception returned in the future that could not be executed because of this?

This, in turn, leads to how to handle errors using futures. I struggle with the general advice that errors should be returned, not thrown, using, for example, either Either or scalar Or. But it seems very difficult if, when calling the future, you need to consider exceptions from the infrastructure, even if everything else is written as exceptions or exceptions. But I will not ask for advice on this matter - it seems to me that he closes this post as "too wide" .: = (

+4
source share
2 answers

Yes and no ... well, maybe we need to dwell on this in detail.

" ", , , , , ( , ..) . . , , , , concurrency stuff: threads.

, - : . , : , .

No part:

, , , , , Or

, . , . 1 100 000 . (, , ). - , , .

. , . , , .

, , . . , , .

- catch try , .

- , Akka, . , ( ).

, , , .

, , , , , , "" .

+3

Future, , , Future. Future.apply PromiseCompletingRunnable . :

class PromiseCompletingRunnable[T](body: => T) extends Runnable {
  val promise = new Promise.DefaultPromise[T]()

  override def run() = {
    promise complete {
      try Success(body) catch { case NonFatal(e) => Failure(e) }
    }
  }
}

, Promise try/catch, Future.apply. Runnable (, ) try/catch, Future. - , , . , , Future .

, , , ( ?). ( ) . , API concurrency , ..

+4

All Articles