SysTutorials

Linux Manual Pages › Section 3

stdbad_typeid(3)

std::bad_typeid – std::bad_typeid

Synopsis

Defined in header <typeinfo>
class bad_typeid : public std::exception;

An exception of this type is thrown when a typeid operator is applied to a dereferenced null pointer value of a polymorphic type.
 std-bad typeid-inheritance.svg
Inheritance diagram

Member functions

              constructs a new bad_typeid object
constructor (public member function)

Inherited from std::exception

Member functions

destructor destroys the exception object
             (virtual public member function of std::exception)
[virtual]

what returns an explanatory string
             (virtual public member function of std::exception)
[virtual]

Example

// Run this code

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

Output:

  Attempted a typeid of NULL pointer!