Python:使用sys.exit或SystemExit的区别和建议 [英] Python: using sys.exit or SystemExit differences and suggestions

查看:85
本文介绍了Python:使用sys.exit或SystemExit的区别和建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在线阅读某些程序员使用 sys.exit ,其他程序员使用 SystemExit

对不起基本问题:

Reading online some programmers use sys.exit, others use SystemExit.
Sorry for the basic question:


  1. 有什么区别?

  2. 何时需要使用SystemExit还是函数内部的sys.exit?

示例

ref = osgeo.ogr.Open(reference)
if ref is None:
    raise SystemExit('Unable to open %s' % reference)

ref = osgeo.ogr.Open(reference)
if ref is None:
    print('Unable to open %s' % reference)
    sys.exit(-1)


推荐答案

没有实际区别,但是示例代码中还有另一个区别-打印进入标准输出,但异常文本进入标准错误(这可能是您想要的)。

No practical difference, but there's another difference in your example code - print goes to standard out, but the exception text goes to standard error (which is probably what you want).

这篇关于Python:使用sys.exit或SystemExit的区别和建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆