Click here to Skip to main content
14,733,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a long sequence of calculations (using variables of type "double") where some operations might result (depending on input values) in "NaN" or "Infinity".
Since I don't want to add something like the following:
if ((double.IsInfinity(result)) || (double.NaN(result))) {
...
}

after each operation, I would like to activate automatic raise of exception for such results, and then just catch the exception to stop after the first "wrong" result.

I tried this:
You can also try turning on "Check for arithmetic overflow/underflow." It is located in your project properties, under "Build->Advanced Build Settings"
When it is turned on, arithmetic exceptions will be thrown for an overflow and underflow (instead of wrapping). It may or may not apply to the ln function. Give it a try.

and this:
To trap where a specific exception is being thrown you could (temporarily) break when the exception is thrown in the debugger.
Select Debug > Exceptions then expand the tree to select Common Language Runtime Exceptions > System > System.ArithmeticException and check the "Thrown" option.

but still got no exceptions raised after a division by zero.
Any suggestions?
Thank you.
Posted
Updated 22-Sep-20 1:41am

1 solution

Hello,

I think you'll need to raise the exception yourself like this :
if ((double.IsInfinity(result)) || (double.NaN(result))) {
   throw new ArithmeticException();
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900