Python Glossary Русский

all Python 3Exceptionsexception

decimal.InvalidOperation

module exceptions

An invalid decimal operation (e.g. an ordering comparison with Decimal('nan'), or 0/0).

Syntax

raise decimal.InvalidOperation

Watch out when parsing input: Decimal("abc") raises InvalidOperation, not the ValueError you get from int("abc"), so a habitual except ValueError around user input will miss it (except ArithmeticError does catch it). The trap for this signal is on in the default context; turn it off via getcontext().traps and the operation quietly yields NaN instead, which then spreads through every later computation with nothing to show for it but a sticky entry in getcontext().flags.

Examples

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

See also

Open in the full glossarydocs.python.orgReport an inaccuracy