Linked exceptions
Message-passing concurrency

Prev:Synchronous sends


A thread can handle exceptions using the standard Factor exception handling mechanism. If an exception is uncaught the thread will terminate. For example:
[ 1 0 / "This will not print" print ] "division-by-zero" spawn

Processes can be linked so that a parent thread can receive the exception that caused the child thread to terminate. In this way 'supervisor' threads can be created that are notified when child threads terminate and possibly restart them.
spawn-linked ( quot name -- thread )


This will create a unidirectional link, such that if an uncaught exception causes the child to terminate, the parent thread can catch it:
[ [ 1 0 / "This will not print" print ] "linked-division" spawn-linked drop receive ] [ "Exception caught." print ] recover

Exceptions are only raised in the parent when the parent does a receive or receive-if. This is because the exception is sent from the child to the parent as a message.