### unexpected() and terminate() Functions

Not all thrown errors can be caught and successfully dealt with by a catch block. In some situations, the best way to handle an exception is to terminate the program. Two special library functions are implemented in C++ to process exceptions not properly handled by catch blocks or exceptions thrown outside of a valid try block. These functions are unexpected() and terminate().

When a function with an exception specification throws an exception that is not listed in its exception specification, the function void unexpected() is called. Next, unexpected() calls a function specified by the set_ unexpected() function. By default, unexpected() calls the function terminate().

In some cases, the exception handling mechanism fails and a call to void terminate() is made. This terminate() call occurs in any of the following situations:

- When terminate() is explicitly called
- When no catch can be matched to a thrown object
- When the stack becomes corrupted during the exception-handling process
- When a system defined unexpected() is called

The terminate() function calls a function specified by the set_terminate() function. By default, terminate calls abort() , which exits from the program.

A terminate function cannot return to its caller, either by using return or by throwing an exception.

C++ Exception Handling Overview

Example of Using the Exception Handling Functions

Catch Block
set_unexpected() and set_terminate() Functions
Exception Specifications
Using Exception Handling
Transferring Control