“Overflow” (Problem)
What will the following code display?
1
var maxInt32 = Int32.MaxValue;
2
var maxDouble = Double.MaxValue;
3
var maxDecimal = Decimal.MaxValue;
4
checked
5
{
6
Console.Write("Checked Int32 increased max: ");
7
try { Console.WriteLine(maxInt32 + 42); }
8
catch { Console.WriteLine("OverflowException"); }
9
Console.Write("Checked Double increased max: ");
10
try { Console.WriteLine(maxDouble + 42); }
11
catch { Console.WriteLine("OverflowException"); }
12
Console.Write("Checked Decimal increased max: ");
13
try { Console.WriteLine(maxDecimal + 42); }
14
catch { Console.WriteLine("OverflowException"); }
15
}
16
unchecked
17
{
18
Console.Write("Unchecked Int32 increased max: ");
19
try { Console.WriteLine(maxInt32 + 42); }
20
catch { Console.WriteLine("OverflowException"); }
21
Console.Write("Unchecked Double increased max: ");
22
try { Console.WriteLine(maxDouble + 42); }
23
catch { Console.WriteLine("OverflowException"); }
24
Console.Write("Unchecked Decimal increased max: ");
25
try { Console.WriteLine(maxDecimal + 42); }
26
catch { Console.WriteLine("OverflowException"); }
27
}
Copied!
Solution
Copy link