Error Handling · Lesson 49 of 76
Error handling
Some errors you can't prevent — a user types letters where you expected a number, a file is missing. Instead of letting the program crash, you handle the error and keep going.
The tool is try / except: try the risky code; if it fails, run the backup plan.
Python
Without the try, int("not a number") would crash the whole program. With it, you stay in control and give a friendly message.
Your turn: change text to "42" and run it — see the try succeed.
You’re reading for free. Sign in to keep your progress and earn a certificate when you finish.Sign in to keep my progress →