In Python, an ArithmeticError
is raised when some arithmetic operation is attempted but cannot be executed. It is a subclass of the Exception
class that represents other generic, non-system-specific exceptions.
ArithmeticError is a base class, with several sub-classes, such as ZeroDivisionError
, OverflowError
, and FloatingPointError
. When an arithmetic operation fails or violates some arithmetic law, the interpreter raises a specific ArithmeticError sub-class type.
Anatomy of the Python ArithmeticError exception
The ArithmeticError exception in Python is raised when we have tried to perform an invalid arithmetic operation. The very first feature of this exception class that stands out is that it is rooted. In simpler terms, you cannot instantiate an object with it; it is merely a parent class that other arithmetic-related exceptions inherit from.
ArithmeticError is the root class of the Arithmetic exception hierarchy. The sub-classes that will be raised, depending on the arithmetic operation in fault, include:
ZeroDivisionError
– It is raised when the second argument passed to the/
or%
standard division or modulo operator is zero.OverflowError
– It is raised when a mathematical operation exceeds the maximum limit of the data type.FloatingPointError
– It is raised when a floating-point operation fails.ValueError
– It is raised when the input passed is not of a valid form.TypeError
– It is raised when an argument is not of a valid type.
Python ArithmeticError Examples
Example 1: ZeroDivisionError
The ZeroDivisionError
exception is raised when the second argument passed to the /
or %
operator is zero. Here’s how an example of how you can raise it manually:
try: x = 1 / 0 except ZeroDivisionError as e: print(f"class: {type(e).__name__}, Reason: {str(e)}")
The output of the above code is:
class: ZeroDivisionError, Reason: division by zero
A similar example could be when you divide a list by zero, which is not a valid operation:
try: x = [1,2,3] / 0 except ZeroDivisionError as e: print(f"class: {type(e).__name__}, Reason: {str(e)}")
The ZeroDivisionError
exception handler will be invoked, with the following output:
class: ZeroDivisionError, Reason: division by zero
Example 2: OverflowError
The OverflowError
exception in Python is raised when a mathematical operation exceeds the maximum limit of the data type. For example:
result = 10 ** 1000 try: print(result) except OverflowError as e: print(f"class: {type(e).__name__}, Reason: {str(e)}")
In the above code, we attempt to calculate 10 to the power of 1000. However, because the result is well outside the integer limits of the computer, the running Python interpreter raises an OverflowError
The output of the above code is:
class: OverflowError, Reason: (34, 'Result too large')
Example 3: FloatingPointError
The FloatingPointError
exception will be raised when a floating point operation fails. A possible situation where a FloatingPointError
might occur is below:
try: result = 1.23 + 4.56 + 7.89 - 4.56 - 7.89 - 1.23 print(result) result2 = 1.23 + 4.56 + 7.89 - 4.56 - 7.89 - 1.23 + 1.7976931348623157e+308 except FloatingPointError as e: print(f"class: {type(e).__name__}, Reason: {str(e)}")
The output of the above code is:
5.329070518200751e-15 class: OverflowError, Reason: int too large to convert to float
Conclusion
The ArithmeticError
class in Python is used to handle all the errors that occur while performing arithmetic operations. It is a subclass of the Exception
class and serves as the base class for other specific exceptions, including ZeroDivisionError
, OverflowError
, FloatingPointError
, ValueError
, and TypeError
.