Can I get the exception from the finally block in python?

前端 未结 4 885
夕颜
夕颜 2020-12-04 23:56

I have a try/finally clause in my script. Is it possible to get the exact error message from within the finally clause?

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 00:29

    No, at finally time sys.exc_info is all-None, whether there has been an exception or not. Use:

    try:
      whatever
    except:
      here sys.exc_info is valid
      to re-raise the exception, use a bare `raise`
    else:
      here you know there was no exception
    finally:
      and here you can do exception-independent finalization
    

提交回复
热议问题