Why in C# is Example A valid, compilable and will just wrap while examples B will not compile?
A
int val = 0;
val = val + Int32.MaxValue +2;
or
int val = Int32.MaxValue;
val++;
B
int val = 0;
val = 2147483647 + 1;
or
int val = 0;
int val = Int32.MaxValue + 1;
I know by default that arithmetic exceptions are not checked by default unless you explicitly do so using checked method, block or attribute in the config. My question relates more to compiler then how an arithmetic exception happens.
ArithmeticException is the base class for the following exceptions: DivideByZeroException, which is thrown in integer division when the divisor is 0. For example, attempting to divide 10 by 0 throws a DivideByZeroException exception.
The arithmetic exception is a type of unusual outcome or unchecked error of the code, which is thrown or raised whenever a wrong mathematical or arithmetic operation appears in the code during run time.
ArithmeticException inherits from the RuntimeException class which means it is an unchecked, runtime exception [2].
An arithmetic exception is an error that is thrown when an invalid arithmetic situation occurs. This usually happens when mathematical calculation errors occur within a program at run-time.
Your B examples are constant-folded at compile time, indicating to the compiler that it's guaranteed to overflow.
Because your A examples use variables, the expressions cannot be (completely) constant-folded, so the compiler can't guarantee that the values will result in an overflow.
For instance...
int val = 0;
// some other thread changes `val` to -5...
val = val + Int32.MaxValue +2; // no overflow
However, if you know that val
won't change, and assign 0 to a const int
:
const int startval = 0;
int val = startval + Int32.MaxValue + 2;
You can get your compile-time overflow check back because the value can be completely determined and therefore constant-folded.
I know that arithmetic exceptions are not checked by default unless you explicitly do so using checked method, block or attribute in the config
You do not know that because that statement is incorrect. And in fact you know it to be incorrect because you've provided a case where your statement is proven false!
I refer you to section 7.6.12 of the C# specification, a portion of which I reproduce here for your convenience:
For non-constant expressions (expressions that are evaluated at run-time) that are not enclosed by any checked or unchecked operators or statements, the default overflow checking context is unchecked unless external factors (such as compiler switches and execution environment configuration) call for checked evaluation.
For constant expressions (expressions that can be fully evaluated at compile-time), the default overflow checking context is always checked. Unless a constant expression is explicitly placed in an unchecked context, overflows that occur during the compile-time evaluation of the expression always cause compile-time errors.
See the spec for further details.
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