1

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
  • 112,704
  • 77
  • 249
  • 231
J Arun Mani
  • 600
  • 2
  • 18

2 Answers2

2

Use this:

type(e).__name__

Or

type(e).__class__.name

Or

type(e).__class__.qualname
Wasif
  • 13,656
  • 3
  • 11
  • 30
1

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

Chris
  • 112,704
  • 77
  • 249
  • 231