Using C++ Exceptions
C++ exceptions deal with the relationship of an application and a class member. When the application calls the class member and an error occurs, the class member informs the application of the error. The class member has done what is known as throwing an exception. The application takes this exception and handles it in a block of code called the catch block or the exception handler. It is in this block of code that the error can be dealt with and the program allowed to recover from this error. Taking a few steps back, the code that originally called the class member had to be included in a try block. The try block makes it possible to return the exception errors to the catch block. It is a guarded body of code that signifies what code is to return exception errors if they occur. Remem ber that code that does not interact with the class does not have to be in a try block.
There are three keywords for exception handling in C++:

try

A try block is a group of C++ statements, normally enclosed in braces { }, which may cause an exception. This grouping restricts exception handlers to exceptions generated within the try block.

catch

A catch block is a group of C++ statements that are used to handle a specific raised exception. Catch blocks, or handlers, should be placed after each try block. A catch block is specified by:

  • The keyword catch
  • A catch expression, which corresponds to a specific type of exception that may be thrown by the try block
  • A group of statements, enclosed in braces { }, whose purpose is to handle the exception

throw

The throw statement is used to throw an exception to a subsequent exception handler. A throw statement is specified with:

  • The keyword throw
  • An assignment expression; the type of the result of the expression determines which catch exception handler receives control

Example program for try-catch and throw
#include<iostream>
using namespace std;
int main()
{
   void func()
   {
     try
  {
                                   throw 1;
  }
  catch(int a)
  {
    cout << "Caught exception number:  " << a << endl;
    return;
  }
  cout << "No exception detected!" << endl;
    return;
}
}
Stack Unwinding

               When an exception is thrown, the runtime mechanism first searches for an appropriate handler in the current scope. If no such handler exists, control is transferred from the current scope to a higher block in the calling chain. This process is iterative: It continues until an appropriate handler has been found. At this point, the stack has been unwound and all the local objects that were constructed on the path from a try block to a throw expression have been destroyed. The stack unwinding process is very similar to a sequence of return statements, each returning the same object to its caller.
int main()
{
 try {
 }
 catch(std::exception& stdexc) {
 }
 catch(...) {
 }
}

Exception Type Match

The type of an exception determines which handler will catch it. The matching rules for exceptions are more restrictive than the matching rules for function overloading. Consider the following example:

try
{
 throw int();
}
catch (unsigned int)
 
{
}

Passing Exception Objects to a Handler
An exception can be passed by value or by reference to its handler. Exceptions that are passed by value are constructed on the stack frame of the caller. When an exception is passed by reference, the handler receives a reference to the exception object. Passing an exception by reference ensures its polymorphic behavior. For example:
#include <cstdio>
class ExBase {/*...*/};
class FileEx: public ExBase {/*...*/};

void Write(FILE *pf)
{
 if (pf == NULL) throw FileEx();
 //... process pf normally
}
int main ()
{
 try
 {
  Write(NULL); //will cause a FileEx exception to be thrown
 }
 //catch ExBase or any object derived from it
 catch(ExBase& exception)
 {
 //diagnostics and remedies
 }
}

Exception Type Match

The type of an exception determines which handler will catch it. The matching rules for exceptions are more restrictive than the matching rules for function overloading. Consider the following example:

try
{
 throw int();
}
// the following block won't catch the exception from
// the previous try block
catch (unsigned int)
 
{

The thrown exception is of type int, but the handler expects an unsigned int. The exception-handling mechanism doesn't consider these to be matching types; therefore, the thrown exception isn't caught.

Exception Specification

A function that might throw an exception can warn its users by specifying a list of the exceptions that it can throw. Exception specifications are particularly useful when users of a function can view its prototype but can't access its source file. Following is an example of specifying an exception:

class Zerodivide{/*..*/};
int divide (int, int) throw