16.2.2 Exceptions
Exception handling is a language feature present in other modern
programming languages. Ada and Java both have exception handling
mechanisms. In essence, exception handling is a means of propogating a
classified error by unwinding the procedure call stack until the error
is caught by a higher procedure in the procedure call chain. A
procedure indicates its willingness to handle a kind of error by
catching it:
|
void foo ();
void
func ()
{
try {
foo ();
}
catch (...) {
cerr << "foo failed!" << endl;
}
}
|
Conversely, a procedure can throw an exception when something goes
wrong:
|
typedef int io_error;
void
init ()
{
int fd;
fd = open ("/etc/passwd", O_RDONLY);
if (fd < 0) {
throw io_error(errno);
}
}
|
C++ compilers tend to implement exception handling in full, or not at
all. If any C++ compiler you may be concerned with does not implement
exception handling, you may wish to take the lowest common denominator
approach and eliminate such code from your project.
|