Error Handling

Error handling is a way of dealing with problems that happen when a program runs.

Sometimes, programs crash because of mistakes like typing the wrong input. Instead of stopping the program completely, error handling lets the program catch these problems and respond safely.

Example 1 - Handling Errors When Adding Numbers

 1try:
 2    numberOne = float(input("Enter a number: "))
 3    numberTwo = float(input("Enter another number: "))
 4
 5    result = numberOne + numberTwo
 6
 7    print(f"Result: {result}")
 8
 9except ValueError:
10    print("You must enter numbers only.")
Show Output
Enter a number: 5
Enter another number: ten
You must enter numbers only.

Explanation

This program asks the user to type in two numbers. It then adds the two numbers together and shows the result on the screen.

The try block contains the code that might cause an error - in this case, when the user enters something that isn’t a number. If that happens, Python will stop running the try block and jump to the except block instead.

The except ValueError: block then displays the message “You must enter numbers only.” This prevents the program from crashing and gives the user clear feedback about what went wrong.

Example 2 - Using a While Loop to Validate User Input

1numberInvalid = True
2
3while numberInvalid == True:
4    try:
5        number = int(input("Enter a number: "))
6        numberInvalid = False
7
8    except ValueError:
9        print("Please enter a number.")
Show Output
Enter a number: ten
Please enter a number.
Enter a number: 1a
Please enter a number.
Enter a number: 10

Explanation

At the start of the program, the variable numberInvalid is set to True. This means that the program assumes the user has not yet entered a valid number.

The while loop will continue running as long as numberInvalid is True. Inside the loop, the program tries to run the code inside the try block. It asks the user to enter a value using the input() function and then attempts to convert that input into an integer using int().

If the user enters something that can be converted into a number, such as 5 or 12, the conversion will succeed. The program then sets numberInvalid to False, which causes the loop to stop.

However, if the user enters something that cannot be turned into a number, such as cat or hello, the program will produce a ValueError. When this happens, the except block runs and displays the message "Please enter a number." on the screen. The loop then starts again, asking the user to try again.