# OverflowException

***

**1. Arithmetic Operations**

```csharp
decimal value1 = 1000000000000000000;
decimal value2 = 1000000000000000000;
try
{
    decimal result = value1 * value2;
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during multiplication.");
}
```

**2. Bitwise Operations**

```csharp
int value1 = int.MaxValue;
int value2 = 1;
try
{
    int result = value1 << value2;
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during bitwise left shift.");
}
```

**3. Array Index**

```csharp
int[] array = new int[10];
try
{
    array[10] = 12;
}
catch (IndexOutOfRangeException ex)
{
    Console.WriteLine("Index out of range.");
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during array index assignment.");
}
```

**4. List Capacity**

```csharp
List<int> list = new List<int>();
try
{
    list.Capacity = int.MaxValue;
}
catch (ArgumentOutOfRangeException ex)
{
    Console.WriteLine("Capacity value out of range.");
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during capacity assignment.");
}
```

**5. Tuple Creation**

```csharp
try
{
    Tuple<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int> tuple = new Tuple<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
}
catch (ArgumentException ex)
{
    Console.WriteLine("Tuple has too many elements.");
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during tuple creation.");
}
```

**6. Stream Position**

```csharp
FileStream stream = new FileStream("test.txt", FileMode.Open);
try
{
    stream.Position = long.MaxValue;
}
catch (IOException ex)
{
    Console.WriteLine("I/O error occurred.");
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during stream position setting.");
}
```

**7. Date and Time Operations**

```csharp
DateTime date1 = DateTime.MaxValue;
TimeSpan span = TimeSpan.FromDays(1);
try
{
    DateTime date2 = date1 + span;
}
catch (ArgumentOutOfRangeException ex)
{
    Console.WriteLine("Date value out of range.");
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during date and time operation.");
}
```

**8. TimeSpan Creation**

```csharp
try
{
    TimeSpan span = new TimeSpan(int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue);
}
catch (ArgumentOutOfRangeException ex)
{
    Console.WriteLine("TimeSpan value out of range.");
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during TimeSpan creation.");
}
```

**9. BitConverter Conversion**

```csharp
byte[] bytes = new byte[4];
try
{
    int value = BitConverter.ToInt32(bytes, 0);
}
catch (ArgumentException ex)
{
    Console.WriteLine("Argument is not a valid byte array for this method.");
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during bit converter conversion.");
}
```

**10. Numeric Parse**

```csharp
string number = "123456789012345678901";
try
{
    int value = int.Parse(number);
}
catch (FormatException ex)
{
    Console.WriteLine("Input string was not in a correct format.");
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during numeric parse.");
}
```

**11. Byte Conversion**

```csharp
byte[] bytes = new byte[256];
try
{
    sbyte value = (sbyte)bytes[0];
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during byte conversion.");
}
```

**12. Decimal Rounding**

```csharp
decimal value = 12345678901234567890.12345678901234567890m;
try
{
    value = Math.Round(value);
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during decimal rounding.");
}
```

**13. Int To Long Cast**

```csharp
int value = int.MaxValue;
try
{
    long value2 = (long)value;
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during integer to long cast.");
}
```

**14. Safe Integer Comparison**

```csharp
int value1 = int.MaxValue;
int value2 = 1000000000;
int result = Math.Max(value1, value2);

if (result == value1)
{
    Console.WriteLine("Overflow occurred during safe integer comparison.");
}
```

**15. Array Length**

```csharp
int[] array = new int[int.MaxValue];
try
{
    int length = array.Length;
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during array length retrieval.");
}
```

**16. BigInteger Operations**

```csharp
BigInteger value1 = BigInteger.One;
BigInteger value2 = BigInteger.Parse("100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
try
{
    BigInteger result = value1 * value2;
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during BigInteger operation.");
}
```

**17. Decimal ToString**

```csharp
decimal value = 12345678901234567890.12345678901234567890m;
try
{
    string result = value.ToString();
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during decimal to string conversion.");
}
```

**18. Int32 Addition**

```csharp
int value1 = int.MaxValue;
int value2 = 1000;
try
{
    int result = value1 + value2;
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during Int32 addition.");
}
```

**19. Checked Integer Addition**

```csharp
int value1 = int.MaxValue;
int value2 = 1000;
try
{
    checked
    {
        int result = value1 + value2;
    }
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during checked integer addition.");
}
```

**20. Unchecked Integer Addition**

```csharp
int value1 = int.MaxValue;
int value2 = 1000;
int result = unchecked(value1 + value2);
Console.WriteLine(result);
```

**21. Checked Integer Multiplication**

```csharp
int value1 = int.MaxValue;
int value2 = 2;
try
{
    checked
    {
        int result = value1 * value2;
    }
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during checked integer multiplication.");
}
```

**22. Unchecked Integer Multiplication**

```csharp
int value1 = int.MaxValue;
int value2 = 2;
int result = unchecked(value1 * value2);
Console.WriteLine(result);
```

**23. Checked Integer Subtraction**

```csharp
int value1 = 0;
int value2 = int.MinValue;
try
{
    checked
    {
        int result = value1 - value2;
    }
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during checked integer subtraction.");
}
```

**24. Unchecked Integer Subtraction**

```csharp
int value1 = 0;
int value2 = int.MinValue;
int result = unchecked(value1 - value2);
Console.WriteLine(result);
```

**25. Checked Integer Division**

```csharp
int value1 = int.MaxValue;
int value2 = 2;
try
{
    checked
    {
        int result = value1 / value2;
    }
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during checked integer division.");
}
```

**26. Unchecked Integer Division**

```csharp
int value1 = int.MaxValue;
int value2 = 2;
int result = unchecked(value1 / value2);
Console.WriteLine(result);
```

**27. Checked Integer Modulus**

```csharp
int value1 = int.MaxValue;
int value2 = 2;
try
{
    checked
    {
        int result = value1 % value2;
    }
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during checked integer modulus.");
}
```

**28. Unchecked Integer Modulus**

```csharp
int value1 = int.MaxValue;
int value2 = 2;
int result = unchecked(value1 % value2);
Console.WriteLine(result);
```

**29. Checked Integer Left Shift**

```csharp
int value = int.MaxValue;
int shift = 1;
try
{
    checked
    {
        int result = value << shift;
    }
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during checked integer left shift.");
}
```

**30. Unchecked Integer Left Shift**

```csharp
int value = int.MaxValue;
int shift = 1;
int result = unchecked(value << shift);
Console.WriteLine(result);
```

**31. Checked Integer Right Shift**

```csharp
int value = int.MinValue;
int shift = 1;
try
{
    checked
    {
        int result = value >> shift;
    }
}
catch (OverflowException ex)
{
    Console.WriteLine("Overflow occurred during checked integer right shift.");
}
```

**32. Unchecked Integer Right Shift**

```csharp
int value = int.MinValue;
int shift = 1;
int result = unchecked(value >> shift);
Console.WriteLine(result);
```

**33. Checked Floating-Point Addition**

```csharp
float value1 = float.MaxValue;
float value2 = 1.0f;
try
{
    checked
    {
        float result = value1 + value2;
    }
}
catch (OverflowException ex)
{
    Console

```
