Based on this answer...
Finding the type of an object in C++
...I wrote this code:
static TVALUE getUpperBou
You are casting to a pointer. bad_cast
is only generated when casting to a reference.
You can just check for nullptr
if casting to a reference isn't desirable.
dynamic_cast
only throws exceptions on failure if its template argument is a reference type.
When it's a pointer type, dynamic_cast
instead evaluates to NULL
on failure.
This is made very clear in the preferred language reference, for which you could simply have Googled:
5c) Otherwise, the runtime check fails. If the
dynamic_cast
is used on pointers, the null pointer value of type _new_type_ is returned. If it was used on references, the exceptionstd::bad_cast
is thrown.
I also don't understand why you expect an int
to be thrown, rather than something that derives std::exception
. Perhaps you were hoping that return
in a try
block jumps to the matching catch
block? It doesn't.