Catch a type error in C++
Question
How do i check if a result is of the right type(int, float, double, etc.) and then throw and catch an exception in case it's not?
Thanks all,
Vlad.
Solution
Could you give more detail about what is giving you "a result" you may be able to determine what you need from there and more likely in a better way.
If all you really want is to check the type, use typeid
.
More info here
Following Daniel's model of editing posts to actually answer the question after stating something else...
From my other comment:
You have to do this BEFORE you have just the result. Checking for overflow after is not a good idea. Do a check on the numbers before adding to see if they will overflow, or restrict input to be less than half the max value of the type
OTHER TIPS
There is no way to know that at runtime with C++. These would be compile-time errors.
To answer your second question, you have to manually check for buffer overflows/underflows or use a more appropriate datatype.
The closest you'll get is dynamic_cast.