Exception

Exceptions provide a way to react to exceptional circumstances (like runtime errors) in the program by transferring control to special functions called handlers.
To catch exceptions a portion of code must be placed under exception inspection. This is done by enclosing that portion of code in a try block. When an exceptional circumstance arises within that block, an exception is thrown that transfers the control to the exception handler. If no exception is thrown, the code continues normally and all handlers are ignored.

The concept of exception handling

            Exceptions are of two kinds, namely, synchronous exceptions and asynchronous exceptions. Errors such as “out of range index” and “overflow” belong to the synchronous type exceptions. The errors that are caused by events beyond the control of the program are called asynchronous exceptions.
            The purpose of exception handling mechanism is to provide means to detect and report an “exceptional circumstances”. So that appropriate action can be taken. The mechanism suggests a separate error handling code that performs the following task.

  1. Find the problem (hit the exception).
  2. Inform that an error has occurred (throw the exception).
  3. Receive the error information (catch the exception).
  4. Take corrective actions (handle the exception).

The error handling code basically consists of two segments, one to detect errors and to throw exception, and the other to catch the exceptions and to make appropriate action.
ProgramFlow
The raising of the imaginary error flag is simply called raising or throwing an error. When an error is thrown the overall system responds by catching the error. Surrounding a block of error-sensitive code with exception handling is called trying to execute a block.
One of the most powerful features of exception handling is that an error can be thrown over function boundaries. This means that if one of the deepest functions on the stack has an error, this error can propagate up to the upper function if there is a trying-block of code there. This allows programmers to put the error handling code in one place, such as the main-function of the program.
Example
An exception is thrown by using the throw keyword from inside the try block. Exception handlers are declared with the keyword catch, which must be placed immediately after the try block.
#include <iostream.h>

using namespace std;

int main () {
  try
  {
    throw 20;
  }
  catch (int e)
  {
    cout << "An exception occurred. Exception Nr. " << e << endl;
  }
  return 0;
}
The code under exception handling is enclosed in a try block. In this example this code simply throws an exception:
throw 20;
A throw expression accepts one parameter (in this case the integer value 20), which is passed as an argument to the exception handler. The exception handler is declared with the catch keyword. It follows immediately the closing brace of the try block. The catch format is similar to a regular function that always has at least one parameter. The type of this parameter is very important, since the type of the argument passed by the throw expression is checked against it, and only in the case they match, the exception is caught.
Multiple handlers (catch expressions) can be chained, each one with a different parameter type. Only the handler that matches its type with the argument specified in the throw statement is executed. If an ellipsis (...) is used as the parameter of catch, that handler will catch any exception no matter what the type of the throw exception is. This can be used as a default handler that catches all exceptions not caught by other handlers if it is specified at last:
try {
  // code here
}
catch (int param) {cout << "int exception";}
catch (char param) {cout << "char exception";}
catch (...) {cout << "default exception";}
In this case the last handler would catch any exception thrown with any parameter that is neither an int nor a char. After an exception has been handled the program execution resumes after the try-catch block, not after the throw statement. It is also possible to nest try-catch blocks within more external try blocks. In these cases, there is a possibility that an internal catch block forwards the exception to its external level. This is done with the expression throw; with no arguments. For example:
try {
  try {
      // code here
  }
  catch (int n) {
      throw;
  }
}
catch (...) {
  cout << "Exception occurred";
}

Standard exceptions

The C++ Standard library provides a base class specifically designed to declare objects to be thrown as exceptions. It is called exception and is defined in the <exception> header file under the namespace std. This class has the usual default and copy constructors, operators and destructors, plus an additional virtual member function called what that returns a null-terminated character sequence (char *) and that can be overwritten in derived classes to contain some sort of description of the exception.