next up previous contents
Next: Constructing functional values Up: Concrete differences Previous: Pattern matching   Contents

Exceptions

They are used to inform about unusual situation in a program, in order to transfer control to block, that can handle it. They can be thought of as member of a huge union:

        union exn {
                // system defined
                void Null_access;
                void Match_failure;
                void Not_found;
                
                // user defined
                string Syntax_error;
                void ICE;
        }

New members are added with exception keyword:

        exception string Syntax_error;
        exception void ICE;

In order to signal unusual situation, you use raise keyword:

        raise Syntax_error["parse error"];
        raise ICE;
        raise ICE[];

At the place, where you know how to handle it, you use try:

        try {
                open_file();
                ...
                parse();
                ...
        } with e {
        case Syntax_error[s]:
                print_msg(s);
        case ICE:
                print_msg("Internal Compiler Error");
        } finally {
                close_file();
        }

First we open some file, then we call parse(), that can raise Syntax_error or ICE in which case we print error message, or something else, but in then control is transfered to upper-level try block. No matter how control leaves try {} block instructions in finally { ... } are executed. So we always close the file.

[[Nope... but they are expected right after modules, which means Real Soon Now. But I guess there will be only very partial support, just for system exceptions, until pattern matching is done]]



Micha³ Moskal 2001-12-03