mike1
mike1

Reputation: 71

Error/Exception with parameters

I would like to know how I can throw exceptions / errors to say that a parameter is null or its not in the .txt file that works as a dictionnary.

The dictionnary it's like that:

"""rule:parameter:value"""
aa:alfa:1
bba:beta:15

I got this for the moment:

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

Upvotes: 1

Views: 223

Answers (2)

wu1meng2
wu1meng2

Reputation: 537

You can use assert with an error message

assert arg is not None, "Parameter is null"

If the assertion fails, an AssertionError with the error message will be raised.

Upvotes: 0

blue note
blue note

Reputation: 29081

Just raise a value error

raise ValueError("you parameter is null or in wrong format")

Upvotes: 1

Related Questions