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"
}
)
上述方法嘗試要麼從源URL [的安裝]或通過共同服用構建和版本號安裝一個構建並查詢構建存儲庫,獲取存儲庫和安裝構建。因此,源url可以是None,或者version_number和build_number可以是None。我寫了相同的錯誤處理。但是我的應用程序無法處理上述情況的錯誤。任何人都可以讓我知道我錯過了什麼嗎?Python的嘗試除了錯誤,程序崩潰
這裏是程序的輸出:
Traceback (most recent call last):
File "test.py", line 18, in <module>
"password":"xyz"
File "test.py", line 7, in install_build
raise ValueError("Please specifiy the value(s) for either the source_url or version number and the build number")
ValueError: Please specifiy the value(s) for either the source_url or version number and the build number
如果我使用和條件,而不是或,它不會打印錯誤消息,我的意思是不引發異常。 – Var
我的情況是,兩者都應該有字符串值。所以如果version_no是無和build_no有價值,「AND」條件失敗 – Var