python: How do I know what type of exception occurred?

后端 未结 15 2064
情书的邮戳
情书的邮戳 2020-11-29 14:56

I have a function called by the main program:

try:
    someFunction()
except:
    print \"exception happened!\"

but in the middle of the ex

15条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 15:19

    Use the below for both Exception type and Exception text

    import sys
    print(str(sys.exc_info()[0]).split(' ')[1].strip('>').strip("'")+"-"+(str(sys.exc_info()[1])))
    

    if you want only exception type: Use -->

    import sys
    print(str(sys.exc_info()[0]).split(' ')[1].strip('>').strip("'"))
    

    Thanks Rajeshwar

提交回复
热议问题