10.5.2. Trapping and Reporting Errors

Gamma provides a pair of functions referred to as the try/catch mechanism, that is very important for debugging. Consider the following simple event loop:

while (t)
{
    next_event();
}
	

This will run until the program exits or an event triggers some code that produces an error condition. There is no protection against errors. Now consider the following setup:

while (t)
{
    try
    {
        next_event();
    }
    catch
    {
        princ("error occurred\n");
    }
}
	  

This setup of try/catch will try to evaluate the block of code contained in the "try" portion and jumps to the "catch" portion when an error occurs. A more effective example of the catch code block is:

while (t)
{
    try
    {
        next_event();
    }
    catch
    {
        princ("internal error: ", _last_error_,
            " calling stack is: ",stack(),"\n");
    }
}
	

This setup provides the developer with information about the last error and the calling stack which led to the last error. Tutorial II provides an example which illustrates the try/catch and protect/unwind mechanisms to get reports on an error.

Another setup that the developer may find useful is to automatically start an interactive session in the case of an error. The example of such a setting can be found in Tutorial II.