[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Catch exception with message?

Piyush Verma <114piyush at> 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