Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch a type error in C++

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.

like image 514
Vlad Avatar asked Oct 20 '25 15:10

Vlad


1 Answers

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

like image 99
Adam W Avatar answered Oct 23 '25 05:10

Adam W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!