Catch exception with message?

Ben Finney ben+python at benfinney.id.au
Fri Jun 3 20:48:33 EDT 2016


Piyush Verma <114piyush at gmail.com> writes:

> But sometimes, we see same type of exception is present with different
> message.Is there a way to capture same exception with message
> filtering? Please help me to do this.

That's a nasty code smell. Why would you want your code to behave
differently depending on what the text of the message is? The message
should not change the semantic meaning of the exception type.

If there are exceptions that *mean* different things (i.e. that require
different handling), they should be different types.

See the Python 3 standard exception hierarchy for a good example.

-- 
 \        “The greatest tragedy in mankind's entire history may be the |
  `\       hijacking of morality by religion.” —Arthur C. Clarke, 1991 |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list