Module 12: Study Guide
Review key concepts from Error Handling & Next Steps
Key Terms
Exception: An error that occurs during program execution (as opposed to syntax errors which are caught before running).
Traceback: Python's error report showing the chain of calls that led to the error, the file, line number, and error type.
try/except: A construct that catches exceptions and runs alternative code instead of crashing.
raise: A keyword to manually trigger an exception.
Debugging: The process of finding and fixing errors (bugs) in your code.
Print debugging: Adding print statements to trace variable values and program flow.
Lesson 1: Common Python Errors
- SyntaxError: Grammar mistake - missing colon, parenthesis, quote
- NameError: Undefined name - typo, undefined variable, missing import
- TypeError: Wrong type - "hello" + 5, wrong number of arguments
- ValueError: Wrong value - int("hello"), unpacking mismatch
- IndexError: List index out of range
- KeyError: Dictionary key not found (use
.get()to avoid) - Always read error messages from the bottom up
Lesson 2: try/except/finally
Execution Order
try: # 1. Try this code first except: # 2a. Runs if error occurs (skips else) else: # 2b. Runs if NO error (skips except) finally: # 3. ALWAYS runs
What to Know
- Always catch specific exceptions, not bare
except: - Use
as eto capture the error message elseruns only when no exception occurredfinallyalways runs - good for cleanupraisecreates your own exceptions- Combine try/except with while loops for input validation
Lesson 3: Debugging Strategies
- Read the traceback: Line number, error type, error message
- Print debugging: Add print() to trace variable values
- Comment out code: Isolate the problematic section
- Rubber duck debugging: Explain code line by line out loud
- Common mistakes: = vs ==, indentation, off-by-one, modifying list while looping
- Take breaks: Fresh eyes often spot bugs faster
Lesson 4: Next Steps
- OOP: Classes and objects - the natural next topic after this course
- Web development: Flask (lightweight) or Django (full-featured)
- Data science: pandas, NumPy, matplotlib
- Automation: File organization, web scraping, report generation
- Best advice: Code every day, build projects, join communities
Review Questions
1. What is the difference between a SyntaxError and a NameError?
2. In what order do try/except/else/finally blocks execute?
3. Why should you catch specific exceptions instead of using bare except?
4. What is rubber duck debugging and why does it work?
5. How do you use the raise keyword?
6. What are three common beginner mistakes in Python?
Common Mistakes to Avoid
- Using bare
except:which catches all errors including ones you want to see - Ignoring error messages instead of reading them carefully
- Not using try/except for user input that could fail
- Putting too much code in the try block (makes it hard to know what failed)
- Not testing edge cases (empty lists, zero values, missing files)
- Forgetting that
=is assignment and==is comparison