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
C# OverflowException
The OverflowException in C# is thrown when an arithmetic operation results in a value that exceeds the range of the target data type. This commonly occurs during type conversions, arithmetic operations, or when parsing strings that contain values outside the acceptable range.
Common Scenarios
OverflowException is typically thrown in the following situations −
Converting a string to an integer when the value exceeds
int.MaxValueorint.MinValueArithmetic operations that result in overflow (in checked context)
Casting between numeric types where the source value is too large for the target type
Using int.Parse() with Large Values
When parsing a string that contains a number larger than the maximum integer value, int.Parse() throws an OverflowException −
Example
using System;
class Demo {
static void Main() {
try {
string str = "757657657657657";
int res = int.Parse(str);
Console.WriteLine("Parsed value: " + res);
}
catch (OverflowException ex) {
Console.WriteLine("OverflowException caught: " + ex.Message);
}
}
}
The output of the above code is −
OverflowException caught: Value was either too large or too small for an Int32.
Using TryParse() to Avoid Exceptions
A safer approach is to use int.TryParse() which returns false instead of throwing an exception −
Example
using System;
class Demo {
static void Main() {
string str = "757657657657657";
int result;
if (int.TryParse(str, out result)) {
Console.WriteLine("Successfully parsed: " + result);
}
else {
Console.WriteLine("Failed to parse. Value is out of range or invalid.");
Console.WriteLine("Consider using long.Parse() for larger values.");
long longResult = long.Parse(str);
Console.WriteLine("Parsed as long: " + longResult);
}
}
}
The output of the above code is −
Failed to parse. Value is out of range or invalid. Consider using long.Parse() for larger values. Parsed as long: 757657657657657
Arithmetic Overflow in Checked Context
OverflowException can also occur during arithmetic operations in a checked context −
Example
using System;
class Demo {
static void Main() {
try {
checked {
int maxValue = int.MaxValue;
Console.WriteLine("Max int value: " + maxValue);
int overflow = maxValue + 1; // This will throw OverflowException
}
}
catch (OverflowException ex) {
Console.WriteLine("Arithmetic overflow detected: " + ex.Message);
}
// Without checked context, overflow wraps around
unchecked {
int maxValue = int.MaxValue;
int wraparound = maxValue + 1;
Console.WriteLine("Unchecked overflow result: " + wraparound);
}
}
}
The output of the above code is −
Max int value: 2147483647 Arithmetic overflow detected: Arithmetic operation resulted in an overflow. Unchecked overflow result: -2147483648
Data Type Ranges
| Data Type | Range | Alternative for Larger Values |
|---|---|---|
| int (Int32) | -2,147,483,648 to 2,147,483,647 | long (Int64) |
| long (Int64) | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | decimal, BigInteger |
| short (Int16) | -32,768 to 32,767 | int, long |
Conclusion
OverflowException occurs when values exceed the range of their target data types. Use TryParse() methods for safer parsing, choose appropriate data types for your value ranges, and consider using checked contexts to detect arithmetic overflows during development.
