Python Fundamentals / Functions and errors
Lesson 34 of 38
Handling errors with try/except
try runs code that might fail, and except catches the error so your program can respond nicely instead of crashing.
Key points
- Catch specific errors like ValueError
- Give the user a helpful message
- finally always runs
try:
n = int(input("Number: "))
except ValueError:
print("That wasn't a number.")Quiz · +10 XP
Which error does int("abc") raise?
Log in to save progress and earn XP.