Skip to content
Python and Data Primer / Python Foundations

Files and Exceptions

A market-data file can be missing, malformed or unsuitable for a calculation. This lesson reads text, converts it into values and checks those values. It also explains when a program can recover from an error and when it should stop. The same steps apply to API responses and need explicit tests.
Learning objectives. By the end of this note you should be able to:
  • open a text file with with so it closes reliably;
  • parse a delimited row into typed values;
  • catch a specific expected exception;
  • avoid swallowing an unexpected programming error.

In this lesson

  1. Read through a managed boundary
  2. Parse, then validate
  3. Catch narrowly
  4. Parsing and validation are separate checks

Before you start

This lesson is part of a free topic. Create a free account, no card needed, to read it in full with worked examples and exercise solutions.

Read it free