9 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, Compiler is name of user module
string Compiler::Syntax_error;
void Compiler::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 Compiler::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]]