Handling errors (exceptions)
When an error happens, Python raises an exception and the program stops. With try / except, you can catch it and react instead of crashing.
You name the type of error you expect, such as ValueError (wrong value) or ZeroDivisionError (division by zero). The finally block runs in every case, whether or not there was an error.
try:
number = int("abc")
except ValueError:
print("That's not a number!")
try:
print(10 / 0)
except ZeroDivisionError as e:
print("Error:", e)
finally:
print("Done")