C++ examples for Statement:try catch
Demonstrates an example of stack unwinding
#include <string> #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; void f1();//from w w w . ja v a 2 s.c om 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; }