I was testing my code and I thought that this piece of code was correct:
while True:
try:
p = Decimal(raw_input(...))
if ...condition... : break
else: raise ValueError
except ValueError:
print "Error! ..."
but it's not, since when I input "a", this is what I get:
File "multiple.py", line 28, in <module>
precision = Decimal(raw_input(...))
File "/usr/lib/python2.7/decimal.py", line 548, in __new__
"Invalid literal for Decimal: %r" % value)
File "/usr/lib/python2.7/decimal.py", line 3872, in _raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: 'a'
ValueError does not catch InvalidOperation. I don't want the program to stop because of that, I want it to keep asking for input until it satisfies the condition. How do I do it?
decimal.InvalidOperation
exception... – Manninoexcept decimal.InvalidOperation
? – CraigcraigheadBaseException
. You can always define more subclasses in that hierarchy for specific purposes. – Gaikwar