I'm using
Decimal.Round(decimal d)
MSDN says it can throw OverflowException
https://msdn.microsoft.com/en-us/library/k4e2bye2(v=vs.110).aspx
I'm not sure how that can happen. I tried looking over the implementation using ilSpy And got until the external implementation of:
// decimal
[SecurityCritical]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void FCallRound(ref decimal d, int decimals);
Does anybody got a clue what input can throw this exception?
When we go further from what you already discovered yourself, we end up in the implementation of the VarDecRound function. This function has exactly one branch where it returns an error code, and that is when its second argument cDecimals
is smaller than zero. This argument indicates the number of decimal digits to round to:
if (cDecimals < 0)
return E_INVALIDARG;
(this kind of assertion is the equivalent of what an ArgumentException
would be in .NET)
As James Thorpe pointed out in a comment on OP, a similar assertion is done further up the call chain, here:
if (decimals < 0 || decimals > 28)
FCThrowArgumentOutOfRangeVoid(...)
Conclusion:
Execution cannot reach the point that would result in throwing the OverflowException
as documented:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With