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.

petezurich
  • 9,280
  • 9
  • 43
  • 57
Haris Muzaffar
  • 404
  • 6
  • 17
  • 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. – rdas May 27 '19 at 06:35
  • Possible duplicate of [How to print an exception in Python 3?](https://stackoverflow.com/questions/41596810/how-to-print-an-exception-in-python-3) – Stephen Rauch May 27 '19 at 06:37

1 Answers1

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

Ranga
  • 620
  • 1
  • 7
  • 9
  • e.args[0] usually contains the message but not necessarily, it may some other information regarding the exception. That is where the issue lies – Haris Muzaffar May 27 '19 at 06:44
  • @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. – Ranga May 27 '19 at 06:51