Python尝试除了错误,程序崩溃

时间:2015-05-29 03:54:21

标签: python python-2.7 error-handling try-catch try-except

  id day   state state_int
1  1   1 healthy         2
2  1   2    sick         1
3  2   1    sick         1
4  2   2    dead         0

上述方法尝试从安装程序的源URL安装构建,或者共同获取构建版本号并查询构建存储库,获取存储库并安装构建。因此,源URL可以是None,或者version_number和build_number可以是None,而不是两者。我已经写了相同的错误处理。但我的应用程序无法处理上述方案的错误。谁能让我知道我错过了什么?

以下是该计划的输出:

def install_build(install_info,source_url=None):
    if source_url is None:
        try:
            version_no = install_info["version_number"]
            build_no = install_info["build_number"]
            if version_no is None or build_no is None:
                raise ValueError("Please specifiy the value(s) for either the source_url or version number and the build number")
        except KeyError:
            print("You are missing either or both of these keys: version number or build number")
        # If the necessary parameters are present, call the method to install the build
        install_build_on_server(version_no,build_no,source_url)

if __name__ == "__main__":
    install_build(    
        {   
            "product_type":"abc",
            "username":"def",
            "version_number":None,
            "build_number":"123",
            "password":"xyz"
        }   
        )

2 个答案:

答案 0 :(得分:1)

if version_no is None or build_no is None:

应该是

if version_no is None and build_no is None:

答案 1 :(得分:0)

使用和检查两者: 如果version_no为None且build_no为None:

因为你传递的version_no是None,它会在(OR)部分引发一个Value错误。要么更改条件,要么在version_no和build_no中传递None以外的值。