c# - Why does this method return double.PositiveInfinity not DivideByZeroException? -
i ran following snippet in vs2015 c# interactive , got weird behavior.
> double divide(double a, double b) . { . try . { . return / b; . } . catch (dividebyzeroexception exception) . { . throw new argumentexception("argument b must non zero.", exception); . } . } > divide(3,0) infinity > 3 / 0 (1,1): error cs0020: division constant 0 > var b = 0; > 3 / b attempted divide zero. >
why did method return infinity while 3 / 0 threw error , 3 / b threw formated error? can force division have thrown error instead of returning infinity?
if reformat method to
double divide(double a, double b) { if ( b == 0 ) { throw new argumentexception("argument b must non zero.", new dividebyzeroexception()); } return / b; }
would new dividebyzeroexception contain same information , structure caught exception would?
it's because use system.double.
as stated msdn dividebyzeroexception thrown integral types , decimal.
that's because hard define "so called" 0 double value.
positiveinfinity results division 0 positive dividend, , negativeinfinity results division 0 negative dividend. (source: msdn again)
dividebyzeroexception not appropriate floating point types. note: can nan
though when attempting divide 0 dividend of zero.
Comments
Post a Comment