Responding to Unexpected Events
Unexpected events may be detected anywhere in a program, but they are typically detected in library functions that interact with the operating system and the external world. Calls to these functions are usually found nested many levels deep in the function call stack.
An unexpected event blocks the forward progress of the program's current computation. The program could choose to halt abruptly when faced with an unexpected event, but if it wants to be able to do anything other than halt (including simply saving work and printing a message), it must abandon the current computation and return to higher-level code that kicks off new computations. It is in this higher-level code that the program can decide whether execution can continue or must be stopped.
There are two ways this can happen. Traditionally, the function that detected the unexpected event can stop what it's doing, manually clean up any resources it's using, and return...