When exactly would a bad_typeid exception be used?
-
10What's wrong with this explanation: http://www.cplusplus.com/reference/std/typeinfo/bad_typeid/ – Mikael Persson Mar 03 '11 at 02:32
-
I'm really looking or a more specific situation in which you would use this exception. – Joshua Mar 03 '11 at 04:34
2 Answers
The bad_typeid exception is thrown by the typeid operator when the operand for typeid is a NULL pointer. In other words, exceptions of class bad_typeid are thrown when typeid is called on a dereferenced null pointer.

- 161,384
- 21
- 275
- 467

- 4,117
- 23
- 25
-
Can you think of any specific examples of when you would use this exception? I see the documentation on those pages of how it looks to use it but I'm just looking for a more specific situation in which it would be used. – Joshua Mar 03 '11 at 04:21
This type of exception generally wouldn't be used. Instead, it only exists so that you can recognize it. If a tester or user sends you a crash report that mentions bad_typeid
, you'll know what sort of bug to look for when you start debugging.
You're not really meant to throw this exception yourself, and I don't think you're even meant to catch it, either. After all, what could you possibly do to resolve the root cause of this error within your program? The way to recover from this type of exception is to find the bug, fix it, recompile, and distribute an updated version of your program that doesn't attempt to use the typeid
operator on invalid values.

- 1
- 1

- 161,384
- 21
- 275
- 467