Setting exit code in Python when an exception is raised

馋奶兔 提交于 2019-11-27 15:33:23

问题


$ cat e.py
raise Exception
$ python e.py
Traceback (most recent call last):
  File "e.py", line 1, in <module>
    raise Exception
Exception
$ echo $?
1

I would like to change this exit code from 1 to 3 while still dumping the full stack trace. What's the best way to do this?


回答1:


Take a look at the traceback module. You could do the following:

import sys, traceback

try:
  raise Exception()
except:
  traceback.print_exc()
  sys.exit(3)

This will write traceback to standard error and exit with code 3.



来源:https://stackoverflow.com/questions/6720119/setting-exit-code-in-python-when-an-exception-is-raised

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!