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

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

    You usually should not catch all possible exceptions with try: ... except as this is overly broad. Just catch those that are expected to happen for whatever reason. If you really must, for example if you want to find out more about some problem while debugging, you should do

    try:
        ...
    except Exception as ex:
        print ex # do whatever you want for debugging.
        raise    # re-raise exception.
    

提交回复
热议问题