ArithmeticError
is simply an error that occurs during numeric calculations.
ArithmeticError
types in Python include:
OverFlowError
ZeroDivisionError
FloatingPointError
These errors are all capable of crashing a code in Python. It is essential to catch an error because you do not want your code to crash as a result of incorrect input from you or a user.
ArithmeticError
The program below will reveal what error will arise from our code.
arithmetic = 5/0print(arithmetic)
You can see that the code above returns an error output that reads as follows:
ZeroDivisionError: division by zero
This is a type of ArithmeticError
.
Mathematically, dividing an integer by zero is wrong, and that is the reason why Python crashes the program and returns an error message.
ArithmeticError
Now, let us see how we can catch any of these errors in our code.
try:arithmetic = 5/0print(arithmetic)except ArithmeticError:print('You have just made an Arithmetic error')
try
, except
block to help us check for errors in our line of codes.arithmetic
.except
to check for an ArithmeticError
and complete the block.ArithmeticError
in our code, we have previously told Python to return a text output instead of crashing our program.