Overflow Handling In C#]

S

Steve

My question is how to handle and where to handle integer overflow
exceptions.

I am developing a C# application and I am debating on whether we
should turn on the "Check for arithmetic overflow" compiler flag. My
concerns are not necessarily security rather logic bugs.

If I turn it on, is there a performance hit? Throughout the program
or only when performing the integer operations?

Do I have to try/catch all of my integer math? If no, how do I catch
them? If yes, why not just use 'check' blocks along with the
try/catch?

Should I deliver my app with the flag on?

Thanks,
Steve
 
W

William Ryan eMVP

Here's a general discussion on it
http://www.knowdotnet.com/articles/checked.html


Steve said:
My question is how to handle and where to handle integer overflow
exceptions.

I am developing a C# application and I am debating on whether we
should turn on the "Check for arithmetic overflow" compiler flag. My
concerns are not necessarily security rather logic bugs.

If I turn it on, is there a performance hit?
The primary thing to worry about is another programmer not knowing the
context and coding it ambiguously.

Throughout the program
or only when performing the integer operations?

As a rule, I think you ought to specify what you are doing in the code
block. As far as where to try/catch stuff, that really depends on a lot of
stuff and how you want to program to behave. Performance is but one
consideration.
Do I have to try/catch all of my integer math? If no, how do I catch
them?
It depends on how you want to deal with it.
If yes, why not just use 'check' blocks along with the
try/catch? That's what I'd do actually.

Should I deliver my app with the flag on?
Again that's a big question. I have a strong bias toward having it on and
basically against anything that may obscure a problem. Personally I'd
rather the app blow up so I can fix the problem then run on for a month with
some subtle flaw , the results of which may or may not even be fixable.
Thanks,
Steve

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top