Catching errors with try and except
An error that happens while a program runs is called an exception. Python raises the exception, and unless something handles it, the program stops and prints a traceback. A program can prepare for an exception with try and except:
try:
tickets = int(input("How many tickets? "))
print(f"Total: {tickets * 15} euros")
except ValueError:
print("That's not a whole number.")
print("Thank you!")
twoHow many tickets? two
That's not a whole number.
Thank you!Python runs the try block. If a line in it raises a ValueError, the rest of the try block is skipped and the except ValueError block runs instead. After that the program carries on below the whole structure, as if nothing had happened. When no exception happens, the except block is skipped.
The except line names the kind of exception it handles. Any other kind still stops the program, which is good: an error you didn't expect should show up, not be hidden.
A fallback value
Sometimes the best reaction to bad input is a sensible default:
text = input("Font size: ")
try:
size = int(text)
except ValueError:
size = 12
print("Using font size", size)
bigFont size: big
Using font size 12Keep the try block small, with only the lines that can fail in the way you expect. Then it's clear what the except is for.
Asking until the input is good
Together with a loop, try and except can keep asking until the user types a number. In a function, return ends the loop as soon as the conversion succeeds:
def read_int(prompt):
while True:
try:
return int(input(prompt))
except ValueError:
print("Please type a whole number.")
year = read_int("Year: ")
print("In ten years it will be", year + 10)
twenty
20.5
2030Year: twenty
Please type a whole number.
Year: 20.5
Please type a whole number.
Year: 2030
In ten years it will be 2040Doing nothing with pass
Sometimes there's nothing to do about an exception except skip the bad value. An except block can't be empty, so it gets pass, the statement that does nothing:
total = 0
for text in ["5", "abc", "12", "", "3"]:
try:
total += int(text)
except ValueError:
pass
print(total)
20