Python Quest

Learning map · Track 9 · intermediate

Exceptions and Debugging

Every real program meets bad data sooner or later: a price typed as 12,50, a missing column in a spreadsheet export, a customer ID that does not exist, a file that ends halfway through a line. Beginners often see the resulting red traceback as a failure. This track teaches you to treat it as information. You will learn to read a traceback from the bottom up and find the exact line and value that caused it, recognise the common built-in exceptions on sight, and decide deliberately which errors your code should handle, which it should report, and which it should let through. You will catch specific exceptions with try/except, use else and finally to keep the success path and the cleanup separate, raise your own exceptions with messages that say what went wrong, and define small exception classes so callers can react to different problems in different ways.

The second half of the track is about robustness and debugging habits. You will write input loops that never crash on a typo, compare the "ask forgiveness" and "look before you leap" styles, use assert for the mistakes only programmers can make, and follow a repeatable debugging process: reproduce, isolate, form a hypothesis, inspect state with prints or the logging module, and confirm the fix. It is written for learners who have finished the tracks on functions, data structures, text and files. The track ends with a mini project: an order importer that works through messy records, collects a clear error for every bad row without stopping, and prints a summary report.

By the end of this track you can

  • Read a traceback bottom-up to find the exception type, its message and the line in your own code that caused it
  • Handle specific exceptions with try, except, else and finally without hiding unrelated bugs
  • Raise built-in and custom exceptions with informative messages, and re-raise or chain them when translating errors
  • Validate user input and messy records robustly, reporting every problem instead of crashing on the first one
  • Choose between EAFP and LBYL, and use assertions only for internal invariants
  • Debug systematically by reproducing, isolating and inspecting state with print() and the logging module

Lessons

  1. 1Reading tracebacksRead a Python traceback from the bottom up to find what went wrong, where, and how the program got there.30 min · 4 exFree
  2. 2Common exceptions and what they meanRecognise ValueError, TypeError, KeyError, IndexError, AttributeError, ZeroDivisionError and NameError, and know where to look for each one's cause.35 min · 4 exMembers
  3. 3Catching exceptions with try and exceptHandle the exceptions you expect with try/except, name them specifically, use the exception object, and avoid bare except.40 min · 4 exMembers
  4. 4else and finallyKeep the success path in else, put guaranteed cleanup in finally, and know the exact order in which the clauses run.35 min · 4 exMembers
  5. 5Raising exceptionsSignal problems from your own functions with raise, choose the right exception type, write messages that help, and re-raise or chain when translating errors.40 min · 4 exMembers
  6. 6Custom exceptionsDefine your own exception classes and small hierarchies so callers can catch exactly the problems they care about.35 min · 4 exMembers
  7. 7Validating inputWrite input loops that never crash on a typo, convert safely, handle the end of input, and report every problem clearly to the user.40 min · 4 exMembers
  8. 8EAFP versus LBYLCompare 'easier to ask forgiveness than permission' with 'look before you leap', and choose between try, an if check and dict.get for each situation.35 min · 4 exMembers
  9. 9Assertions and invariantsUse assert to state what must be true inside your program, catch your own bugs early, and know why assertions must never validate user input.30 min · 4 exMembers
  10. 10A debugging strategyFind bugs systematically: reproduce, isolate, hypothesise and test, inspect state with print(), and record what a program does with the logging module.45 min · 4 exMembers
  11. 11Mini project: a robust order importerBuild an order importer that works through messy CSV records, collects a clear error for every bad row without crashing, and prints a summary report.60 min · 5 exMembers