我想知道如何抛出异常/错误来说明参数为null或不在字典中的.txt文件中的参数。
字典是这样的:
"""rule:parameter:value"""
aa:alfa:1
bba:beta:15
我现在知道了这个:
def get(rule_name,parameter_name):
try:
with open("parameters.txt", "r") as infile:
for line in infile:
if line.startswith(rule_name.lower()) and line.split(":")[1] == parameter_name.lower():
return line.split(":")[2]
except Value:
print "Error"
if __name__=="__main__":
print(get("aa","alfa")) #return the value associated to the rule and the parameter
答案 0 :(得分:1)
只需引发值错误
raise ValueError("you parameter is null or in wrong format")