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.
bad_cast
You can just check for nullptr if casting to a reference isn't desirable.
nullptr