Chapter 2. Exceptions

Table of Contents

What is exceptional?
Goals of exception handling
Setjump/longjump
Components of exception handling
Exception hierarchies
Exceptions in the standard library
Constructors
Destructors
Resource problems
Exception specification
Exception safety

Error-handling in modern programming languages. From longjmp to C++ exceptions. Standard exceptions. Exception hierarchy. Resource management. Exception safe programming. Exceptions in constructors. Exception guaraties in the standard library.

What is exceptional?

There are situations in runtime, where program runs in exceptional way. This could be handle in many different ways: using boolean return values, assertions or exceptions.

Handling exceptional situations:


struct record { ... };

record r;
extern int errno;

FILE *fp;

if ( (fp = fopen( "fname", "r")) != NULL )
{
    fprintf( stderr, "can't open file %s\n", "fname");
    errno = 1;
}
else if ( ! fseek( fp, 0L, n*sizeof(r)) )
{
    fprintf( stderr, "can't find record %d\n", n);
    errno = 2;
}
else if ( 1 != fread( &r, sizeof(r), 1, fp) )
{
    fprintf( stderr, "can't read record\n");
    errno = 3;
}
else ...


//============================================
//
//  asserts
//

#include <cassert>

int main()
{
    ...
    assert( ptr );
    ...
}