Python Glossary Русский

all Python 3Exceptionsexception

decimal.DivisionImpossible

module exceptions

Decimal integer division cannot produce a correct result.

Syntax

raise decimal.DivisionImpossible

This is about integer division rather than the plain / operator: it fires when the integer part produced by // or divmod would need more digits than the context precision allows. It subclasses InvalidOperation, whose trap is enabled in the default context, so you genuinely get an exception — but it is InvalidOperation, not DivisionImpossible, and except decimal.DivisionImpossible will not catch it; catch decimal.InvalidOperation. Untrap it and the operation returns NaN instead. The cure is a larger precision, not a try/except.

Examples

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

See also

Open in the full glossarydocs.python.orgReport an inaccuracy