Learn Python in 30 Days — Day 17: Error Handling & Debugging Mindset
Learn Python in 30 Days — Day 17: Error Handling & Debugging Mindset
Welcome to Day 17 of the Learn Python in 30 Days series!
Up to now, your code has mostly worked as long as the user behaves.
They type a number when you expect a number.
They don’t rename or delete files you’re trying to open.
Real life isn’t that kind.
Today you’ll learn how to:
-
Use
try/exceptto catch and handle errors (exceptions) -
Deal with two super-common problems:
-
ValueError— bad input / wrong data type -
FileNotFoundError— missing files
-
-
Start thinking like a debugger: What could go wrong, and how will I deal with it?
Step 1 – What Are Exceptions?
When Python hits a problem it can’t handle, it raises an exception and stops your program.
Example:
If the user types hello instead of a number:
Key parts to notice:
-
ValueError→ the type of problem -
The message after it → extra details
-
The traceback → where the error happened
Right now, Python just crashes.
Our job today is to catch these errors and respond more helpfully.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Step 2 – Your First try/except
try / except lets you say:
“Try this code. If a specific error happens, run some backup code instead.”
The basic pattern for this code is as follows:
Concrete example with int():
Now if the user types hello, your program doesn’t crash.
Instead, it prints a friendly message and continues.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Step 3 – Handling ValueError (Bad Input)
Any time you convert user input (e.g. with int() or float()), there’s a risk of ValueError.
Let’s make a safe number input helper:
Use it like this:
What’s happening:
-
The
while True:loop keeps going -
If
int(text)works → wereturn valueand exit the function -
If it raises
ValueError→ we catch it and show a helpful message
This is defensive programming where you assume people will type nonsense and your code is ready for it.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Step 4 – Handling FileNotFoundError (Missing Files)
Next common problem: working with files that don’t exist (yet).
If notes.txt doesn’t exist, you’ll get:
Let’s handle that error with the try/except routines: -
Now the program doesn’t crash, try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
But we can be even smarter: if the file doesn’t exist, ask if we should create it.
You’ve just made your code more user-friendly and robust.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Step 5 – A Quick Look at else and finally
try / except has two optional friends: else and finally.
-
else→ runs if no exception happened -
finally→ runs always, error or not (good for cleanup)
Example:
For now, try + except is enough.
Just know these exist; we’ll lean on them more as programs get bigger.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Step 6 – Debugging Mindset: Don’t Fear Errors
Today is also about how you think when something breaks.
Instead of:
“Ahh, it crashed! Python is broken!”
Try:
“Interesting. What exactly went wrong? Where? Why?”
When you see an error:
-
Read the exception type
-
ValueError,FileNotFoundError,TypeError, etc.
-
-
Read the message
-
It often tells you exactly what was wrong.
-
-
Look at the last line of the traceback
-
That line is usually where the problem actually happened.
-
-
Reproduce the error
-
Can you make it happen again with the same input? That’s good — now you can fix it.
-
-
Add temporary print()s
-
Print variables right before the crash to see what they contain.
-
Example of simple “print debugging”:
You’d see b = 0 before getting ZeroDivisionError.
Now you know you need to handle zero differently.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Step 7 – Common try/except Mistakes to Avoid
1. Catching everything with a bare except:
This is like putting tape over the “check engine” light in your car.
Better:
Or, if you really need to catch anything:
…but don’t just silently ignore it.
2. Making the try block too big
Bad practice don't do this, keep it simple:
If anything else goes wrong (like dividing by zero), it’ll still say “Invalid number.”
Better practice is to keep the risky line(s) tight:
Now each error is handled with the right message.
Step 8 – Mini-Project: Safer Temperature Converter (v2)
Let’s upgrade the Temperature Converter from Day 15 to be more robust.
Goal:
-
Ask the user to convert from Celsius to Fahrenheit
-
Use
try/exceptto handle bad input -
Log conversions to a file, handling
FileNotFoundErrorif needed
What you’re practising here:
-
ValueErrorhandling for user input -
File writing with defensive thinking
-
Keeping logic in functions (Days 15–16)
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Next Up — Day 18 – Working with Files
Read/write text files. Mini-project: Simple Notes App.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
You can see the full series here Learn Python in 30 Days series!
Hope you have enjoyed this post, thanks Matty









Comments
Post a Comment