Catch type error in C ++

How can I check if the result is of the correct type (int, float, double, etc.) and then throw and catch an exception if it is not?

Thanks everyone,

Vlad.

+2


source to share


3 answers


Could you please give more details on what gives you the "result", you can determine what you need from there, and more likely in a better way.

If you really want to check the type, use typeid

. More details here

Following Daniel's post editing model to actually answer the question by pointing out something else ...



From my other comment:

You must do this before you simply have a result. Checking for overflow after is not a good idea. Check for numbers before adding to see if they will overflow or limit input should be less than half the maximum value type

+4


source


There is no way to know what at runtime with C ++. These were compile time errors.



To answer the second question, you need to manually check for buffer overflows or overflows, or use a more appropriate data type.

+4


source


The closest you will get is dynamic_cast .

0


source







All Articles