Skip to content
Tactiverge

Learning Rooms/○ Level 3 · Innovator/Perception and signals

Python Tutorial: Using Try/Except Blocks for Error Handling

Corey Schafer · Length: see YouTube · captions on YouTube · nothing loads until you press play

By the end you can…

  • Use try / except ValueError to handle a line that cannot be converted to a number.
  • Add range (plausibility) checks based on the sensor's rated range.
  • Count and report rejected data rather than silently discarding it.
?Predict▶Watch✓Practise⚙Build

Watch it all: no segment has been checked by hand for this video yet.

1.Predict before you press play

A log line is -999.0, a common 'no reading' code. What does float("-999.0") do?

Pick one before you press play. A wrong guess is fine: it is what makes the video stick.

2.See the idea first

Play with this diagram for a minute (borrowed from the lesson “Test the edges: boundary values”, on the same idea). It gives you something to hang the video on.

Loading the interactive…

Can't use the diagram? Read a description instead

The boundary tester keeps a table of test temperatures with the specification's answer, the selected version's answer and pass or fail. Adding the boundary tests (−0.5, 0, 0.5, 44.5, 45, 45.5) shows: Version A fails only at 0; Version B passes all; Version C fails only at 45. Typical values such as 20 °C pass for all three versions.

3.Watch, actively

How try and except let a program handle bad input, such as a malformed line in a log, without crashing.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

Watch for…

  • → Watch how try and except catch a specific error, and why catching only the error you expect is safer than catching everything.

Pause whenever you like. Rewatching a hard minute is a good habit, not a failure.

Can't stream it? Read the idea instead

Real data contains bad lines: missing values, text where a number should be. A try block attempts the risky step and an except block says what to do if a specific error happens, so one malformed reading is logged and skipped instead of crashing the whole program.

What the video covers (our own note, written when we verified it): How try and except let a program handle bad input, such as a malformed line in a log, without crashing.

Written by Tactiverge in our own words, not a transcript. The full lesson below explains the idea without the video.

Read it instead

Jot down one thing that surprised you and one question you still have.

Private: saved only on this device, never sent to Tactiverge.

4.Practise what you just saw

These come from the lesson that use this video and are checked on the server. Wrong answers get a hint and another try.

Practice 1

Why is replacing bad lines with 0 a bad idea?

Practice 2

Why write except ValueError rather than a bare except?

Practice 3 · a fresh version every time

A log has the lines ["-999.0","25.8","-999.0","21.4","21.6","19.1","20.0","--"]. The program keeps a line only if float(line) works and -40 <= value <= 125 (the sensor's rated range, °C); otherwise it skips it. How many readings are kept?

readings

Practice 4

Which check rejects the line "130.5" from a sensor rated −40 to 125 °C?

6.Where engineers use it

Industrial and scientific data pipelines always have this stage, often called input validation or data cleaning, and keep counts of rejected records as a health signal for the sensors.

From the applied view in the lesson Catch bad data before it bites. Checked by automated tests only.

7.Build something with it

Follow-up project

Extend parse_reading to reject nan, then write four asserts (a good line, ERR, blank and −999.0). Use it in your log program and print how many lines were rejected out of the total.

Where this video lives

In the course catalogue: CSE-210 Python for engineers

The video is by Corey Schafer, embedded from YouTube with privacy-enhanced mode and never autoplayed. The room around it (prompts, notes, practice, projects) is Tactiverge's, written with AI assistance and checked by automated tests only.