About catching ANY exception

后端 未结 8 1163
一整个雨季
一整个雨季 2020-11-22 07:23

How can I write a try/except block that catches all exceptions?

8条回答
  •  醉梦人生
    2020-11-22 07:51

    I've just found out this little trick for testing if exception names in Python 2.7 . Sometimes i have handled specific exceptions in the code, so i needed a test to see if that name is within a list of handled exceptions.

    try:
        raise IndexError #as test error
    except Exception as e:
        excepName = type(e).__name__ # returns the name of the exception
    

提交回复
热议问题