Python Glossary Русский

all Python 3Exceptionsexception

decimal.DivisionUndefined

module exceptions

Decimal division 0/0 was attempted (the result is undefined).

Syntax

raise decimal.DivisionUndefined

It is specifically the 0/0 case: dividing a non-zero value by zero raises DivisionByZero instead. Although the class inherits from both InvalidOperation and ZeroDivisionError, what reaches your except is InvalidOperation, not this class — DivisionUndefined is only the name of the condition. So neither except ZeroDivisionError nor except decimal.DivisionUndefined catches 0/0; write except decimal.InvalidOperation. The InvalidOperation trap is on by default; remove it and the operation quietly yields NaN.

Examples

import decimal
try:
    raise decimal.DivisionUndefined
except decimal.DivisionUndefined as e:
    print(type(e).__name__)   # → DivisionUndefined

See also

Open in the full glossarydocs.python.orgReport an inaccuracy