diff --git a/akka-docs/scala/futures.rst b/akka-docs/scala/futures.rst index 8d8c2771f5..331bb1cdba 100644 --- a/akka-docs/scala/futures.rst +++ b/akka-docs/scala/futures.rst @@ -198,12 +198,17 @@ Scalaz Akka also has a `Scalaz module `_ for a more complete support of programming in a functional style. -Exceptions (TODO) ------------------ +Exceptions +---------- -Handling exceptions. +Since the result of a ``Future`` is created concurrently to the rest of the program, exceptions must be handled differently. It doesn't matter if an ``Actor`` or the dispatcher is completing the ``Future``, if an ``Exception`` is caught the ``Future`` will contain it instead of a valid result. If a ``Future`` does contain an ``Exception``, calling ``get`` will cause it to be thrown again so it can be handled properly. -Fine Tuning (TODO) ------------------- +It is also possible to handle an ``Exception`` by returning a different result. This is done with the ``failure`` method. For example: -Dispatchers and timeouts +.. code-block:: scala + + val future = actor !!! msg1 failure { + case e: ArithmeticException => 0 + } + +In this example, if an ``ArithmeticException`` was thrown while the ``Actor`` processed the message, our ``Future`` would have a result of 0. The ``failure`` method works very similarly to the standard try/catch blocks, so multiple ``Exception``\s can be handled in this manner, and if an ``Exception`` is not handled this way it will be behave as if we hadn't used the ``failure`` method.