0

I am trying to resolve exceptions. In python2, i used to write like this:

        except (Exception,InternalError,SQLAlchemyError) as e:
        message = e.message;

But in python3 it gives an error that attribute message is not found. Now i tried this:

        except (Exception,InternalError,SQLAlchemyError) as e:
        message = e[0]

But how do I know which argument e[0], e[1] etc of the exception will hold the message? i need only the message and not all the arguments of the exception.

2
  • Use isinstance. But maybe if you need to differentiate between these different types of exceptions, they should not be part of the same except block. Commented May 27, 2019 at 6:35
  • Possible duplicate of How to print an exception in Python 3? Commented May 27, 2019 at 6:37

1 Answer 1

0

Instead of e.message, python3 now has e.args which is a tuple of args. This allows developers to return multiple arguments. However if only one argument is passed then essentially e.message is equivalent to e.args[0]

Here is the documentation

Sign up to request clarification or add additional context in comments.

2 Comments

e.args[0] usually contains the message but not necessarily, it may some other information regarding the exception. That is where the issue lies
@HarrisMuzaffar True, but e.args[0] will usually have the most important info and most errors/exceptions only pass one argument. As for this with multiple arguments, str(e) essentially concatenates them into a string with some minor formatting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.