Peter17
Peter17

Reputation: 3102

Catching an Integer overflow exception in Visual studio

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

Answers (2)

Adam Houldsworth
Adam Houldsworth

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:

enter image description here

As an aside, the second set of brackets in (byte)(bigNumber) are not required: (byte)bigNumber.

Upvotes: 4

Ivan Golović
Ivan Golović

Reputation: 8832

You can use the compiler "checked" option, http://msdn.microsoft.com/en-us/library/h25wtyxf.aspx

Upvotes: 2

Related Questions