Reputation: 33
I'm new to Python. Please forgive me if my question sounds stupid.
The codes below will raise an error but the error is not captured by try/except. I've gone through them many times but couldn't see what is the problem.
Would appreciate it very much if any Gurus here can show the problem to me.
Thanks for your time.
import decimal
try:
Amount = str(decimal.Decimal('2.675a').quantize(decimal.Decimal('.01'), rounding=decimal.ROUND_HALF_UP))
print Amount
except ValueError:
print 'Error'
The error I've got is:
File "C:\Python27\lib\decimal.py", line 548, in __new__
"Invalid literal for Decimal: %r" % value)
File "C:\Python27\lib\decimal.py", line 3866, in _raise_error
raise error(explanation)
InvalidOperation: Invalid literal for Decimal: '2.675a'
Upvotes: 2
Views: 2030
Reputation: 43899
The error being raised by your code fragment is a decimal.InvalidOperation
exception. This exception is not a subclass of ValueError
so does not match your except
clause.
I would suggest reading the tutorial section on errors and exception handling for more an overview of how to handle errors in Python code.
Upvotes: 5