Catch exception with message?
On Sat, 4 Jun 2016 09:14 am, Piyush Verma wrote:
> Generally we catch exception using
> except Exception as e:
> But sometimes, we see same type of exception is present with different
That suggests that whoever wrote the code doesn't know what they're doing.
Intentionally giving the same error message for different exception types
is poor design.
> Is there a way to capture same exception with message
> filtering? Please help me to do this.
Error messages are not part of the public API of Python's built-ins and
standard library. That means that error messages are subject to change at
any time, without warning. There is no backwards compatibility requirement
for the error message to stay the same.
In principle that means that the same function might use a different error
message each time you run it. But in practice, it means that even bug-fix
releases of Python can change error messages.
So don't do this, it is a terrible idea. You should never rely on the error
message of an exception.
But if you insist:
except Exception as e:
if e.args = "An error occurred":
print("could you be any less specific?")