ExceptionHandlers - laforge49/Asynchronous-Functional-Programming GitHub Wiki
Trapping exceptions can be a bit problematic in AsyncFP because the control flow is not always predictable. So exception handlers are used. And these must be provided for each class of request to be processed. If no exception handler is specified for a given class of request, then the default handler is used, which rethrows the exception in the context of the request processing method which sent the request that is currently being processed.
Lets say we have a request class SNE, a request processing method se and an exception handling method exh. Here is the code for binding the SNE class to exh and se:
bind(classOf[SNE], exh(se))
(Exception handlers can also be used with transactions in a similar way.)
Writing an exception handler is easy enough, except for the method signature:
def exh(mf: (AnyRef, Any => Unit) => Unit)(msg: AnyRef, rf: Any => Unit) {
exceptionHandler(msg, rf, mf) {
(ex, mailbox) =>
println("S got exception " + ex.toString)
rf(null)
}
}