class
<typeinfo>
std::bad_cast
Exception thrown on failure to dynamic cast
Type of the exceptions thrown by dynamic_cast
when it fails the run-time check performed on references to polymorphic class types.
The run-time check fails if the object would be an incomplete object of the destination type.
Its member what returns a null-terminated character sequence identifying the exception.
Some functions in the standard library may also throw this exception to signal a type-casting error.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// bad_cast example
#include <iostream> // std::cout
#include <typeinfo> // std::bad_cast
class Base {virtual void member(){}};
class Derived : Base {};
int main () {
try
{
Base b;
Derived& rd = dynamic_cast<Derived&>(b);
}
catch (std::bad_cast& bc)
{
std::cerr << "bad_cast caught: " << bc.what() << '\n';
}
return 0;
}
|
Possible output:
bad_cast caught: St8bad_cast
|
Exception safety
No-throw guarantee: no members throw exceptions.
See also
- exception
- Standard exception class (class
)