當typeid例外狀況。
語法
catch (bad_typeid)
statement
備註
bad_typeid的介面為:
class bad_typeid : public exception
{
public:
bad_typeid();
bad_typeid(const char * _Message = "bad typeid");
bad_typeid(const bad_typeid &);
virtual ~bad_typeid();
bad_typeid& operator=(const bad_typeid&);
const char* what() const;
};
下列範例顯示 typeid 擲 回bad_typeid例外狀況 的運算符。
// expre_bad_typeid.cpp
// compile with: /EHsc /GR
#include <typeinfo>
#include <iostream>
class A{
public:
// object for class needs vtable
// for RTTI
virtual ~A();
};
using namespace std;
int main() {
A* a = NULL;
try {
cout << typeid(*a).name() << endl; // Error condition
}
catch (bad_typeid){
cout << "Object is NULL" << endl;
}
}
輸出
Object is NULL