Programs rarely fail gracefully; they crash. When Python hits a wall, it raises an exception. Think of this as your program waving its hands and shouting for help instead of silently producing garbage data.
The Mental Model
An exception is like a waiter dropping a tray in a busy restaurant. The kitchen doesn't just keep cooking; it pauses, assesses the mess, and decides whether to clear it or call for help. Your code needs that same pause-and-recover mechanism.
Why It Matters
You will use this all the time: when reading what a user types, or loading a JSON file. If a sensor sends the text "error" instead of a number, your program shouldn't die; it should note the problem and keep running.
Reading the Traceback
def divide(a, b):
return a / b
def main():
result = divide(10, 0)
print(result)
main()When this runs, Python prints a traceback. Read it bottom-up. The last line names the error type (
ZeroDivisionError) and message. The lines above show exactly where you were when things broke.Catching Exceptions
def safe_divide(a, b):
try:
return a / b
except ZeroDivisionError:
print('Cannot divide by zero')
return None
print(safe_divide(10, 2)) # 5.0
# shows Cannot divide by zero, then None
print(safe_divide(10, 0))The
try block holds risky code. The except block catches only that specific exception. If the division succeeds, Python skips the except. This keeps your program alive.else and finally
def process(data):
try:
value = int(data)
except ValueError:
print('Invalid number')
else:
print(f'Got: {value}')
finally:
print('Done processing')
process('42')else runs only if the try block succeeds without raising an error. It is perfect for code that depends on a successful operation. finally always runs, error or not, so it is the place for tidying up that must happen either way, like the Done message above.