Catch any error in Python

前端 未结 8 1659
一个人的身影
一个人的身影 2021-02-01 14:24

Is it possible to catch any error in Python? I don\'t care what the specific exceptions will be, because all of them will have the same fallback.

8条回答
  •  一生所求
    2021-02-01 15:08

    The following only worked for me (both in PY2 and PY3):

    try:
      # (Anything that produces any kind of error)
    except:
      ertype = sys.exc_info()[0]  # E.g. 
      description = sys.exc_info()[1]   # E.g. [Errno 13] Permission denied: ...
      # (Handle as needed ) 
    

提交回复
热议问题