ArithmeticException Class
Namespace: System
Assembly: mscorlib.dll (or System.Runtime.dll in .NET Core)
Overview
The ArithmeticException class is the base class for exceptions that occur during arithmetic, casting, or conversion operations.
Inheritance
System.Object → System.Exception → System.SystemException → System.ArithmeticException
Constructors
Show Constructors ▼
| Signature | Description |
|---|---|
ArithmeticException() | Initializes a new instance of the ArithmeticException class. |
ArithmeticException(string message) | Initializes with a custom error message. |
ArithmeticException(string message, Exception innerException) | Initializes with a custom error message and a reference to the inner exception that caused this exception. |
Properties
Inherits all properties from System.Exception. No additional properties are defined.
Methods
Inherits all methods from System.Exception. No additional methods are defined.
Remarks
This exception is typically thrown when an invalid arithmetic operation occurs, such as a division by zero or an overflow. Specific derived exceptions include DivideByZeroException and OverflowException.
Example
// Example of catching an ArithmeticException
using System;
class Demo
{
static void Main()
{
try
{
int a = int.MaxValue;
int b = 2;
int result = checked(a * b); // Causes overflow
}
catch (ArithmeticException ex)
{
Console.WriteLine($"Arithmetic error: {ex.Message}");
}
}
}