0

If I have the code:

try:
  1 / 0

except Exception as e:
  my_error = e
  #e is sent to the application user end

It returns the text for e but not the exception name as well. In the case of the code I need this for, the console isn't open at the time and the exception is sent through to the user end when it is caught. At the moment when an exception occurs I can see the error text, but how do I get the name as well.

In the case of the example I'd like for my_error to be

ZeroDivisionError: #error text

instead of my_error only being

#error text
John Muir
  • 55
  • 1
  • 3
  • `type(e).__name__` for the exception name. `str(e)` for the message. Put 'em together however you like. – kindall May 03 '19 at 18:27

2 Answers2

2

If you are looking at just the description of the error, you are looking at.

 err_str = str(e)

But if you are also looking for exception name along with description, you are looking at.

 err_str = repr(e)
Devesh Kumar Singh
  • 19,316
  • 5
  • 17
  • 37
2

use below code in exception block.

e.__class__.__name__
mkrana
  • 404
  • 4
  • 10
  • 1
    While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – nircraft May 03 '19 at 19:40
  • @nircraft Thanks for the suggestion. will add details. – mkrana May 10 '19 at 05:42