[Python-ideas] Let try-except check the exception instance
Terry Reedy
tjreedy at udel.edu
Thu May 31 03:19:27 EDT 2018
On 5/31/2018 12:47 AM, Danilo J. S. Bellini wrote:
> Hi!
> I was working on handling some exceptions from external software
> (e.g. database constraint triggers)
> switching the handler based on the messages that had been sent.
> Today we can do something like (running on Python 3.6.5):
>
>
>>>> try:
> ... # [...]
> ... session.commit() # Here it raises!
> ... # [...]
> ... except DatabaseError as exc:
> ... msg = get_db_error_msg_from_exception(exc)
> ... if msg == "beyond_limit":
> ... # [...]
> ... elif msg == "no_funds":
> ... # [...]
> ... else:
> ... raise
>
>
> That works,
Yes, it works perfectly well, AND it exposes the fact that your code
depends on the message, which I think is a good thing.
As Stephen said, messages are intentionally not part of the defined API.
As a matter of curtesy, we usually restrict message changes to new
versions and do not backport the change. An exception may be made if we
decide that a message is sufficiently erroneous that is likely misleads
people. In any case, message dependent code may be version dependent.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list