# ArithmeticException

***

**1. Division by Zero**

```csharp
int x = 10;
int y = 0;
try
{
    int result = x / y;
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Division by zero is not allowed.");
}
```

**2. Integer Overflow**

```csharp
int x = int.MaxValue;
try
{
    int result = x + 1;
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Integer overflow has occurred.");
}
```

**3. Integer Underflow**

```csharp
int x = int.MinValue;
try
{
    int result = x - 1;
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Integer underflow has occurred.");
}
```

**4. Floating-Point Division by Zero**

```csharp
float x = 10.0f;
float y = 0.0f;
try
{
    float result = x / y;
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Division by zero is not allowed.");
}
```

**5. Floating-Point Overflow**

```csharp
float x = float.MaxValue;
try
{
    float result = x * 2;
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Floating-point overflow has occurred.");
}
```

**6. Floating-Point Underflow**

```csharp
float x = float.MinValue;
try
{
    float result = x / 2;
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Floating-point underflow has occurred.");
}
```

**7. Divide by Zero in a Loop**

```csharp
int[] numbers = { 1, 2, 3, 0, 4, 5 };
try
{
    for (int i = 0; i < numbers.Length; i++)
    {
        Console.WriteLine(10 / numbers[i]);
    }
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Division by zero occurred in the loop.");
}
```

**8. Incrementing an Integer Beyond Its Maximum Value**

```csharp
int x = int.MaxValue;
try
{
    x++;
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Integer overflow has occurred.");
}
```

**9. Decrementing an Integer Beyond Its Minimum Value**

```csharp
int x = int.MinValue;
try
{
    x--;
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Integer underflow has occurred.");
}
```

**10. Using a Negative Number in a Square Root Calculation**

```csharp
double x = -10.0;
try
{
    double result = Math.Sqrt(x);
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Square root of a negative number is not allowed.");
}
```

**11. Using a Negative Number in a Logarithm Calculation**

```csharp
double x = -10.0;
try
{
    double result = Math.Log(x);
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Logarithm of a negative number is not allowed.");
}
```

**12. Wrap-Around in a Circular Buffer**

```csharp
int bufferSize = 10;
int[] buffer = new int[bufferSize];
int index = 0;

try
{
    while (true)
    {
        buffer[index++] = 10;
        index %= bufferSize;
    }
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Circular buffer wrap-around occurred.");
}
```

**13. Invalid Matrix Index**

```csharp
int[,] matrix = new int[3, 3];
int x = 4;
int y = 2;
try
{
    int value = matrix[x, y];
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Invalid matrix index.");
}
```

**14. Array Index Out of Bounds**

```csharp
int[] array = { 1, 2, 3 };
int index = 3;
try
{
    int value = array[index];
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Array index out of bounds.");
}
```

**15. Conversion from a String to a Numeric Type**

```csharp
string input = "abc";
int x;
try
{
    x = int.Parse(input);
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Cannot convert string to numeric type.");
}
```

**16. Invalid Operation in a Mathematical Expression**

```csharp
int x = 1;
int y = 2;
try
{
    int result = x - y + "a";
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Invalid operation in mathematical expression.");
}
```

**17. Overflow in a Bitwise Operation**

```csharp
int x = int.MaxValue;
int y = -1;
try
{
    int result = x & y;
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Overflow in bitwise operation.");
}
```

**18. Underflow in a Bitwise Operation**

```csharp
int x = int.MinValue;
int y = 1;
try
{
    int result = x | y;
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Underflow in bitwise operation.");
}
```

**19. Shift Overflow**

```csharp
int x = int.MaxValue;
int y = 1;
try
{
    int result = x << y;
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Shift overflow has occurred.");
}
```

**20. Shift Underflow**

```csharp
int x = int.MinValue;
int y = -1;
try
{
    int result = x >> y;
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Shift underflow has occurred.");
}
```

**21. Pointer Arithmetic Out of Bounds**

```csharp
unsafe
{
    int* ptr = &x;
    try
    {
        ptr--; // Attempt to access memory beyond the beginning of the array
    }
    catch (ArithmeticException ex)
    {
        Console.WriteLine("Pointer arithmetic out of bounds.");
    }
}
```

**22. Invalid Decimal Separator**

```csharp
decimal x;
try
{
    x = decimal.Parse("1,000.00"); // Invalid decimal separator
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Invalid decimal separator.");
}
```

**23. Divide by Zero in a Stored Procedure**

```csharp
using System.Data;
using System.Data.SqlClient;

try
{
    using (var connection = new SqlConnection("connectionString"))
    {
        var command = new SqlCommand("StoredProcedureName", connection);
        command.Parameters.AddWithValue("@parameter1", 10);
        command.Parameters.AddWithValue("@parameter2", 0);
        connection.Open();
        command.ExecuteNonQuery();
    }
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Division by zero occurred in the stored procedure.");
}
```

**24. Overflow in a Custom Function**

```csharp
double CalculateArea(double radius)
{
    try
    {
        return Math.PI * radius * radius;
    }
    catch (ArithmeticException ex)
    {
        throw new OverflowException("Area calculation overflow has occurred.");
    }
}
```

**25. Underflow in a Custom Function**

```csharp
double CalculateVolume(double radius, double height)
{
    try
    {
        return Math.PI * radius * radius * height;
    }
    catch (ArithmeticException ex)
    {
        throw new UnderflowException("Volume calculation underflow has occurred.");
    }
}
```

**26. Divide by Zero in a LINQ Query**

```csharp
var numbers = new List<int> { 1, 2, 3, 0, 4, 5 };
try
{
    var result = numbers.Select(x => 10 / x);
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Division by zero occurred in the LINQ query.");
}
```

**27. Overflow in a Lambda Expression**

```csharp
var numbers = new List<int> { 1, 2, 3, 4, 5 };
try
{
    var result = numbers.Select(x => Math.Pow(2, x));
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Overflow occurred in the lambda expression.");
}
```

**28. Underflow in a Delegate**

```csharp
delegate double CalculateAreaDelegate(double radius);

CalculateAreaDelegate calculateArea = radius => Math.PI * radius * radius;
try
{
    calculateArea(0.00000000000000000000000000000000000001);
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Underflow occurred in the delegate.");
}
```

**29. Overflow in a Task**

```csharp
Task.Run(() =>
{
    try
    {
        int x = int.MaxValue;
        x++; // Overflow
    }
    catch (ArithmeticException ex)
    {
        Console.WriteLine("Overflow occurred in the task.");
    }
});
```

**30. Underflow in a Thread**

```csharp
Thread thread = new Thread(() =>
{
    try
    {
        int x = int.MinValue;
        x--; // Underflow
    }
    catch (ArithmeticException ex)
    {
        Console.WriteLine("Underflow occurred in the thread.");
    }
});
```

**31. Divide by Zero in a Parallel Loop**

```csharp
Parallel.For(0, 10, (i) =>
{
    try
    {
        int result = 10 / i;
    }
    catch (ArithmeticException ex)
    {
        Console.WriteLine("Division by zero occurred in the parallel loop.");
    }
});
```

**32. Overflow in a Parallel LINQ Query**

```csharp
var numbers = new List<int> { 1, 2, 3, 4, 5 };
try
{
    var result = numbers.AsParallel().Select(x => Math.Pow(2, x));
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Overflow occurred in the parallel LINQ query.");
}
```

**33. Underflow in a PLINQ Delegate**

```csharp
delegate double CalculateAreaDelegate(double radius);

CalculateAreaDelegate calculateArea = radius => Math.PI * radius * radius;
try
{
    var result = numbers.AsParallel().Select(calculateArea);
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Underflow occurred in the PLINQ delegate.");
}
```

**34. Divide by Zero in a Data Binding Context**

```csharp
public class ViewModel
{
    public int X { get; set; }
    public int Y { get; set; }
}

var viewModel = new ViewModel();
Binding binding = new Binding("Value", viewModel, "X");
try
{
    binding.ProvideValue(viewModel); // Y is 0
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Division by zero occurred in the data binding context.");
}
```

**35. Overflow in a Compiled Expression**

```csharp
Expression<Func<int, int>> expression = x => x * x;
Func<int, int> compiledExpression = expression.Compile();
try
{
    compiledExpression(int.MaxValue);
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Overflow occurred in the compiled expression.");
}
```

**36. Underflow in a Dynamic Method**

```csharp
DynamicMethod method = new DynamicMethod("CalculateArea", typeof(double), new[] { typeof(double) });
ILGenerator il = method.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Ret);
Delegate calculateArea = method.CreateDelegate(typeof(CalculateAreaDelegate));
try
{
    ((CalculateAreaDelegate)calculateArea)(0.00000000000000000000000000000000000001);
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Underflow occurred in the dynamic method.");
}
```

**37. Divide by Zero in a Native Function**

```csharp
[DllImport("NativeLibrary.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int Divide(int x, int y);

int x = 10;
int y = 0;
try
{
    int result = Divide(x, y);
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Division by zero occurred in the native function.");
}
```

**38. Overflow in an Interop Function**

```csharp
[DllImport("NativeLibrary.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int Multiply(int x, int y);

int x = int.MaxValue;
int y = 10;
try
{
    int result = Multiply(x, y);
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Overflow occurred in the interop function.");
}
```

**39. Underflow in an Unsafe Function**

```csharp
unsafe
{
    int* ptr = &x;
    try
    {
        *ptr = 0; // Underflow
    }
    catch (ArithmeticException ex)
    {
        Console.WriteLine("Underflow occurred in the unsafe function.");
    }
}
```

**40. Divide by Zero in a Parallel Query**

```csharp
var numbers = new List<int> { 1, 2, 3, 0, 4, 5 };
try
{
    var result = numbers.AsParallel().Select(x => 10 / x);
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Division by zero occurred in the parallel query.");
}
```

**41. Overflow in a Reactive Extension Sequence**

```csharp
IObservable<int> sequence = Observable.Range(1, 10);
try
{
    sequence.Select(x => Math.Pow(2, x)).Subscribe();
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Overflow occurred in the reactive extension sequence.");
}
```

**42. Underflow in a Reactive Extension Operator**

```csharp
IObservable<int> sequence = Observable.Range(1, 10);
try
{
    sequence.Select(x => 0.00000000000000000000000000000000000001 / x).Subscribe();
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Underflow occurred in the reactive extension operator.");
}
```

**43. Divide by Zero in a Task Factory**

```csharp
Task.Factory.StartNew(() =>
{
    try
    {
        int result = 10 / 0;
    }
    catch (ArithmeticException ex)
    {
        Console.WriteLine("Division by zero occurred in the task factory.");
    }
});
```

**44. Overflow in a ThreadPool Queue**

```csharp
ThreadPool.QueueUserWorkItem(x =>
{
    try
    {
        int result = int.MaxValue * 2;
    }
    catch (ArithmeticException ex)
    {
        Console.WriteLine("Overflow occurred in the thread pool queue.");
    }
});
```

**45. Underflow in a Custom Exception**

```csharp
public class CustomException : Exception
{
    public CustomException(string message) : base(message) { }
}

try
{
    throw new CustomException("Underflow occurred in the custom exception.");
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Underflow occurred in the custom exception.");
}
```

**46. Divide by Zero in a COM Interop Interface**

```csharp
[ComImport]
[Guid("00000000-0000-0000-0000-000000000000")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IComInterface
{
    [PreserveSig]
    int Divide(int x, int y);
}

var comInterface = new CComObject<IComInterface>();
int x = 10;
int y = 0;
try
{
    int result = comInterface.Object.Divide(x, y);
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Division by zero occurred in the COM interop interface.");
}
```

**47. Overflow in a JavaScriptInterop Function**

```csharp
[JSInvokable]
public static int Multiply(int x, int y)
{
    try
    {
        return x * y; // Overflow
    }
    catch (ArithmeticException ex)
    {
        return 0;
    }
}
```

**48. Underflow in a C++/CLI Function**

```csharp
namespace Native
{
    public class NativeClass
    {
        [DllImport("NativeLibrary.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern double Divide(double x, double y);
    }
}

double x = 0.00000000000000000000000000000000000001;
double y = 10;
try
{
    double result = Native.NativeClass.Divide(x, y);
}
catch (ArithmeticException ex)
{
    Console.WriteLine("Underflow occurred in the C++/CLI function.");
}
```

**49. Divide by Zero in a Xamarin.Forms Page**

```csharp
using Xamarin.Forms;

public class MyPage : ContentPage
{
    public MyPage()
    {
        var button = new Button
        {
            Text = "Divide by Zero"
        };
        button.Clicked += async (sender, e) =>
        {
            try
            {
                int result = 10 / 0;
            }
            catch (ArithmeticException ex)
            {
                await DisplayAlert("Error", "Division by zero occurred.", "OK");
            }
        };

        Content = button;
    }
}
```

**50. Overflow in an ASP.NET MVC Action**

```csharp
public class HomeController : Controller
{
    public ActionResult Index()
    {
        int x = int.MaxValue;
        int y = 10;
        try
        {
            int result = x * y;
        }
        catch (ArithmeticException ex)
        {
            return View("Error");
        }

        return View();
    }
}
```
