Add exception handling section
This commit is contained in:
parent
c0cecfc716
commit
13e98f0fa7
1 changed files with 11 additions and 6 deletions
|
|
@ -198,12 +198,17 @@ Scalaz
|
||||||
|
|
||||||
Akka also has a `Scalaz module <scalaz>`_ for a more complete support of programming in a functional style.
|
Akka also has a `Scalaz module <scalaz>`_ 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.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue