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

后端 未结 15 2132
情书的邮戳
情书的邮戳 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:07

    Unless somefunction is a very bad coded legacy function, you shouldn't need what you're asking.

    Use multiple except clause to handle in different ways different exceptions:

    try:
        someFunction()
    except ValueError:
        # do something
    except ZeroDivision:
        # do something else
    

    The main point is that you shouldn't catch generic exception, but only the ones that you need to. I'm sure that you don't want to shadow unexpected errors or bugs.

提交回复
热议问题