Geas MUD, enter the Aventure!
Geas Home | Play the Game

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.2.8 catch/throw: Error handling in runtime

It happens now and then that you need to make function calls you know might result in a runtime error. For example you might try to clone an object (described later) or read a file. If the files aren't there or your privileges are wrong you will get a runtime error and execution will stop. In these circumstances it is desirable to be able to intercept the error and either display some kind of message or perform other actions instead. The special LPC function operator catch() will do this for you. It returns 1 (true) if an error occurs during evaluation of the given function and 0 (false) otherwise.

 
int catch(function)
e.g.
    if (catch(tail("/d/Relic/fatty/hidden_donut_map")))
    {
        write("Sorry, not possible to read that file.\n");
        return;
    }

It's also possible to cause error interrupts. This is particularly useful when you want to notify the user of an unplanned for event that occurred during execution. Typically you want to do this in the 'default' case of a switch statement, unless (naturally) you use default as a sort of catch-it-all position. In any case throw() will generate a runtime error with the message you specify. A catch() statement issued prior to calling the function that uses throw() will naturally intercept the error as usual.

 
throw(mixed info)
e.g.
    if (test < 5)
        throw("The variable 'test' is less than 5\n");



This document was generated by Ronny Wikh on July, 8 2003 using texi2html