As you can see from the stack trace, the exception raised is not DivisionUndefined
but InvalidOperation
. So, in order to suppress the exception, you should catch InvalidOperation
:
from decimal import Decimal, InvalidOperation
try:
Decimal('0')/Decimal('0')
except InvalidOperation:
print('spam')
If you really want to handle only the DivisionUndefined
case, I guess that you can do something like this:
from decimal import Decimal, DivisionUndefined, InvalidOperation
try:
Decimal('0')/Decimal('0')
except InvalidOperation as err:
if err.args[0][0] is DivisionUndefined:
print('spam')
else:
raise err
But I would not recommend that as neither the structure of the err.args
nor the DivisionUndefined
itself is documented.
And BTW, if you only want to supress the exception, you might want this:
from decimal import Decimal, InvalidOperation, getcontext
c = getcontext()
c.traps[InvalidOperation] = False
Decimal('0')/Decimal('0')