首页 > 解决方案 > 不提供异常的错误代码?

问题描述

def main():

    try:

        exit_code = 0

        # Some code that might raise an exception

    except FileNotFoundError as error:
        exit_code = error.errno
    except (ValueError, TypeError) as error:
        print(error)
    finally:
        sys.exit(exit_code)


if __name__ == "__main__":
    main()

ValueError并且TypeError不要提供错误代码,因为 pylint 给了我以下错误:

Instance of 'ValueError' has no 'errno' memberpylint(no-member)
Instance of 'TypeError' has no 'errno' memberpylint(no-member) 

当我想退出脚本并将错误代码提供给 时sys.exit(),应该为不返回错误代码的异常提供什么值?

标签: pythonpython-3.xexception

解决方案


您提供什么退出状态取决于您。

如果程序成功完成,则返回 0(有时称为 EXIT_SUCCESS)被广泛接受。失败时返回 1 (EXIT_FAILURE) 也很常见。


推荐阅读