bad_typeid 例外狀況
當 typeid 的運算元為 NULL 指標時,會由 typeid 運算子擲回 bad_typeid 例外狀況。
catch (bad_typeid)
statement
備註
bad_typeid 的介面為:
class bad_typeid : public exception
{
public:
bad_typeid(const char * _Message = "bad typeid");
bad_typeid(const bad_typeid &);
virtual ~bad_typeid();
};
下列範例顯示擲回 bad_typeid 例外狀況的 typeid。
// expre_bad_typeid.cpp
// compile with: /EHsc /GR
#include <typeinfo.h>
#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