0

How to get the name of the exception in try..except block of Python?

try:
    #code that throws errors
except Exception as e:
    #A broad except block to catch all errors
    #Handling the error

Sometimes when there are a lot of exceptions to handle, catching Exception seems easy (though discouraged). But I'm not able to find the name of the exception, is there any way to do it?

Chris
  • 93,263
  • 50
  • 204
  • 189
J Arun Mani
  • 590
  • 1
  • 16

2 Answers2

1

Assuming by "name" you mean "type", try type(e) in your except block.

Chris
  • 93,263
  • 50
  • 204
  • 189
1

Use this:

type(e).__name__

Or

type(e).__class__.name

Or

type(e).__class__.qualname
programmer365
  • 12,641
  • 3
  • 7
  • 28