Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Checked vs Unchecked Exceptions in C#
The checked and unchecked keywords in C# control whether arithmetic operations throw exceptions when overflow occurs. By default, C# ignores arithmetic overflow in most contexts, but you can explicitly enable or disable overflow checking using these keywords.
In a checked context, arithmetic overflow throws an OverflowException. In an unchecked context, overflow is ignored and the result wraps around to the valid range.
Syntax
Following is the syntax for checked operations −
int result = checked(expression);
checked {
// multiple statements
}
Following is the syntax for unchecked operations −
int result = unchecked(expression);
unchecked {
// multiple statements
}
Using Checked Context
The checked keyword enables overflow checking, causing an OverflowException when arithmetic operations exceed the data type's range −
Example
using System;
class Program {
public static void Main() {
int maxValue = int.MaxValue;
Console.WriteLine("Max int value: " + maxValue);
try {
int result = checked(maxValue + 1);
Console.WriteLine("Result: " + result);
}
catch (OverflowException ex) {
Console.WriteLine("Overflow Exception: " + ex.Message);
}
try {
checked {
int a = maxValue;
int b = 10;
int sum = a + b;
Console.WriteLine("Sum: " + sum);
}
}
catch (OverflowException ex) {
Console.WriteLine("Block Overflow: " + ex.Message);
}
}
}
The output of the above code is −
Max int value: 2147483647 Overflow Exception: Arithmetic operation resulted in an overflow. Block Overflow: Arithmetic operation resulted in an overflow.
Using Unchecked Context
The unchecked keyword disables overflow checking, allowing values to wrap around when they exceed the data type's range −
Example
using System;
class Program {
public static void Main() {
int maxValue = int.MaxValue;
Console.WriteLine("Max int value: " + maxValue);
int result = unchecked(maxValue + 1);
Console.WriteLine("Unchecked result: " + result);
unchecked {
int a = maxValue;
int b = 10;
int sum = a + b;
Console.WriteLine("Unchecked sum: " + sum);
}
// Demonstrating wrap-around behavior
byte maxByte = byte.MaxValue;
Console.WriteLine("Max byte value: " + maxByte);
byte wrappedResult = unchecked((byte)(maxByte + 1));
Console.WriteLine("Wrapped byte result: " + wrappedResult);
}
}
The output of the above code is −
Max int value: 2147483647 Unchecked result: -2147483648 Unchecked sum: -2147483639 Max byte value: 255 Wrapped byte result: 0
Comparison of Checked vs Unchecked
| Aspect | Checked | Unchecked |
|---|---|---|
| Overflow Behavior | Throws OverflowException | Wraps around silently |
| Performance | Slightly slower due to checking | Faster (default behavior) |
| Use Case | When data integrity is critical | When performance is priority |
| Default Context | Must be explicitly enabled | Default in most contexts |
Compiler Options and Default Behavior
You can control the default overflow checking behavior using the /checked compiler option. When enabled, all arithmetic operations are checked by default unless explicitly marked as unchecked −
Example with Default Unchecked Behavior
using System;
class Program {
public static void Main() {
int maxValue = int.MaxValue;
// Default behavior - unchecked
int defaultResult = maxValue + 1;
Console.WriteLine("Default (unchecked): " + defaultResult);
// Explicitly checked
try {
int checkedResult = checked(maxValue + 1);
Console.WriteLine("Checked: " + checkedResult);
}
catch (OverflowException) {
Console.WriteLine("Checked operation threw overflow exception");
}
}
}
The output of the above code is −
Default (unchecked): -2147483648 Checked operation threw overflow exception
Conclusion
The checked and unchecked keywords provide explicit control over arithmetic overflow behavior in C#. Use checked when data integrity is critical and you need to detect overflow conditions, and use unchecked when performance is important and wrap-around behavior is acceptable.
