C++ Exception Library - bad_typeid


Advertisements

Description

This exception thrown on typeid of null pointer.

Declaration

Following is the declaration for std::bad_typeid.

class bad_typeid;

C++11

class bad_typeid;

Parameters

none

Return Value

none

Exceptions

No-throw guarantee − no members throw exceptions.

Example

In below example for std::bad_typeid.

#include <iostream>
#include <typeinfo>
 
struct S { 
   virtual void f();
}; 
 
int main() {
   S* p = nullptr;
   try {
      std::cout << typeid(*p).name() << '\n';
   } catch(const std::bad_typeid& e) {
      std::cout << e.what() << '\n';
   }
}

The sample output should be like this −

std::bad_typeid
exception.htm
Advertisements