Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to catch ArithmeticError Exception in Python?

If I run the following line of code, I am getting an error.

x = 10/0

How to catch this error and know its type?


1 Answer
Rajendra Dharmkar

ArithmeticError Exception is the base class for all errors that occur for numeric calculations. It is the base class for those built-in exceptions like: OverflowError, ZeroDivisionError, FloatingPointError

We can catch the exception in given code as follows

import sys
try:
7/0
except ArithmeticError as e:
print e
print sys.exc_type
print 'This is an example of catching ArithmeticError'

OUTPUT

integer division or modulo by zero
<type 'exceptions.ZeroDivisionError'>
This is an example of catching ArithmeticError

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.