Share via


bad_typeid 例外狀況

bad_typeid擲回例外狀況的 typeid 運算子時的運算元typeid是一個 NULL 指標。

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();
};

下列範例所示typeid運算子擲回bad_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;
   }
}

Output

Object is NULL

請參閱

參考

執行階段型別資訊

C + + 關鍵字