Is there a way to prevent a SystemExit exception raised from sys.exit() from being caught?

后端 未结 3 837
醉话见心
醉话见心 2020-11-29 23:58

The docs say that calling sys.exit() raises a SystemExit exception which can be caught in outer levels. I have a situation in which I want to definitively and unquestionabl

3条回答
  •  醉话见心
    2020-11-30 00:28

    You can call os._exit() to directly exit, without throwing an exception:

    import os
    os._exit(1)
    

    This bypasses all of the python shutdown logic, such as the atexit module, and will not run through the exception handling logic that you're trying to avoid in this situation. The argument is the exit code that will be returned by the process.

提交回复
热议问题