close

[Solved] Python decimal.InvalidOperation

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python decimal.InvalidOperation in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python decimal.InvalidOperation Error Occurs?

Today I get the following error Python decimal.InvalidOperation in python.

How To Solve Python decimal.InvalidOperation Error ?

  1. How To Solve Python decimal.InvalidOperation Error ?

    To Solve Python decimal.InvalidOperation Error The signal exceptions in the decimal module are all subclasses of the decimal.DecimalException exception class.

  2. Python decimal.InvalidOperation

    To Solve Python decimal.InvalidOperation Error The signal exceptions in the decimal module are all subclasses of the decimal.DecimalException exception class.

Solution 1

The signal exceptions in the decimal module are all subclasses of the decimal.DecimalException exception class. You can catch any of the following exceptions to deal with the InvalidOperation exception, from the specific to the (very) generic:

decimal.InvalidOperation
decimal.DecimalException
ArithmeticError
Exception
BaseException

ValueError is not in that hierarchy, but catching Exception would also catch ValueError since it is a subclass. Generally speaking, you rarely want to catch BaseException since that catches all possible exceptions, including SystemExit and MemoryError.

You can catch multiple exceptions in the same handler:

from decimal import Decimal, DecimalException

while True:
    try:
        p = Decimal(raw_input(...))
        if ...condition... : break
        else: raise ValueError
    except (ValueError, DecimalException):
        print "Error! ..."

would catch both your original ValueError exceptions, and all signal exceptions raised by the decimal module.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read