Skip to content Skip to sidebar Skip to footer

Handling Errors Using A Extra Function

Is it possible to make error handling functions so that I don't have to make error handling where the inputs are? For example: def error_handler(a,b,c): while True: try

Solution 1:

You can create an input function that tests for the validity of the input.

In the following example, test for an int (but you can change that to the type you need.

definput_with_error_handler(txt):
    whileTrue:
        try:
            raw_value = input(txt)
            returnint(raw_value)
        except ValueError:
            print(f"{raw_value} must be an int")
    

a = input_with_error_handler("enter an int: ")
b = input_with_error_handler("enter an int: ")
c = input_with_error_handler("enter an int: ")

Post a Comment for "Handling Errors Using A Extra Function"