C++ Exception stack unwinding

Description

C++ Exception stack unwinding

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

void f1();/*from w w w  .j  ava 2 s.co  m*/
void f2();
void f3();

class Obj
{
  public:
    Obj(char c) : label(c)
    { cout << "Constructing object " << label << endl;}
    ~Obj()
    { cout << "Destructing object " << label << endl; }

  protected:
    char label;
};

int main(int nNumberofArgs, char* pszArgs[])
{
    f1();

    return 0;
}

void f1()
{
    Obj a('a');
    try
    {
        Obj b('b');
        f2();
    }
    catch(float f)
    {
        cout << "Float catch" << endl;
    }
    catch(int i)
    {
        cout << "Int catch" << endl;
    }
    catch(...)
    {
        cout << string("Generic catch") << endl;
    }
}

void f2()
{
    try
    {
        Obj c('c');
        f3();
    }
    catch(string msg)
    {
        cout << "String catch" << endl;
    }
}

void f3()
{
    Obj d('d');
    throw 10;
}



PreviousNext

Related