The standard exceptions of C++ implementation: code example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info:

To do: try-catch with NULL pointer

To show: The standard exceptions handling C++ program example with try-catch construct

 

 

 

// Standard exceptions program example

#include <iostream>

#include <exception>

#include <typeinfo>

using namespace std;

 

class Test1

{

virtual int Funct() {};

};

 

int main (void)

{

try {

Test1 * var = NULL;

typeid (*var);

}

 

catch (std::exception& typevar)

{

cout<<"Exception: "<<typevar.what()<<endl;

}

return 0;

}

 

Output example:

 

Exception: Attempted a typeid of NULL pointer!

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index