The bad_typeid C++ exception program 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: Trying and catching the bad_typeid C++ exception

To show: The bad_typeid usage in C++ programming

 

// bad_typeid

#include <typeinfo>

#include <iostream>

using namespace std;

 

class Test

{

public:

// object for a class needs vtable for the rtti

Test();

virtual ~Test();

};

 

int main(void)

{

Test *ptrvar = NULL;

try {

// the error condition

cout<<typeid(*ptrvar).name()<<endl;

}

catch (bad_typeid)

{

cout<<"The object is NULL"<<endl;

}

}

 

Output example:

 

The object is NULL

Press any key to continue . . .

 

 

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