Reputation: 3102
How can I make the Visual studio to throw an integer overflow exception?
I know I can use a checked
statement explicitly. But I want to make this behavior default in my solution (at least during debugging).
int bigNumber= 12345;
byte overflowException = (byte)(bigNumber); //want to see an exception here
Upvotes: 2
Views: 1860
Reputation: 64547
According to the docs there is a /checked
compiler option for turning this on as default.
The /checked compiler option lets you specify checked or unchecked context for all integer arithmetic statements that are not explicitly in the scope of a checked or unchecked keyword.
In the project properties for the project, go to the "Build" tab and click the "Advanced..." button. On this window is a "Check for arithmetic overflow/underflow" checkbox:
As an aside, the second set of brackets in (byte)(bigNumber)
are not required: (byte)bigNumber
.
Upvotes: 4
Reputation: 8832
You can use the compiler "checked" option, http://msdn.microsoft.com/en-us/library/h25wtyxf.aspx
Upvotes: 2