This exception occurs when a division by zero is carried out for integral or decimal values and can be avoided by the application checking for a zero denominator upfront. Recall that floating-point types have infinity and Not a Number (NaN) values defined that will be used for the result of division-by-zero attempts.

The second and third lines here give rise to this exception, regardless of the use of a checked context; the same holds for the other integral and decimal data types:

int n = 1, d = 0;int div = n / d;int rem = n % d;

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.