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?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
J Arun Mani
  • 620
  • 3
  • 20

2 Answers2

2

Use this:

type(e).__name__

Or

type(e).__class__.name

Or

type(e).__class__.qualname
Wasif
  • 14,755
  • 3
  • 14
  • 34
1

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

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257