Error Handling with try-except

Introduction

Up to this point, most of your Python scripts have assumed everything goes right: the file exists, the user types a number, and the denominator is never zero. Real engineering scripts do not get that luxury. A sensor log may be missing, a CSV column may contain text where a float was expected, or a calculation may divide by zero partway through a batch of data. Python signals these runtime problems by raising exceptions, and if nothing handles them, the program stops immediately with a traceback.

Exception handling gives you a structured way to anticipate those failures and respond to them. In this topic you will learn what an exception actually is, how the basic try-except block separates risky code from recovery code, and how the optional else and finally clauses let you run code only when no error occurred or always run cleanup such as closing a file. You will also see how to capture the exception object itself so you can report a useful message instead of a bare crash, and how to raise exceptions deliberately when your own function receives invalid input, such as a negative length or an out-of-range temperature.

As you watch the videos below, pay attention to three things: which specific exception types are being caught (catching a bare Exception hides bugs), the exact order in which try, except, else, and finally execute, and how the presenter uses the exception message to explain what went wrong. Then connect it back to Module 4 as a whole, since file I/O is where most of your exception handling will live, and try the same examples yourself with deliberately broken input.

Video 1: Exception Handling in Python | Python Tutorial - Day #36

CodeWithHarry · 10:53 · 674,912 views · 3y ago · captions — Watch on YouTube

About this video

This video introduces Python exceptions and the basic try-except structure, showing how code that would normally crash can be caught and handled instead. Expect demonstrations of common error types and how to write except clauses that respond to them, including accessing the error information Python provides. It is the natural starting point for the first several subtopics.

Why this video

CodeWithHarry is a widely followed Python instructor whose day-by-day tutorial series builds concepts in small, well-sequenced steps, making this a clear and accurate introduction to what an exception is and how try-except works.

Video 2: Finally keyword in Python | Python Tutorial - Day #37

CodeWithHarry · 8:05 · 400,752 views · 3y ago · captions — Watch on YouTube

About this video

A direct follow-up that focuses on the finally keyword and how it fits alongside try and except. It should demonstrate that finally runs whether or not an exception occurred, which is exactly the behavior you want for cleanup tasks like closing a file or releasing a resource. Watch for the execution order relative to the except block and any return statements.

Why this video

It targets the else and finally subtopic specifically, and because it continues the same series the examples and style carry over directly from the previous video.

Video 3: Raising custom errors in Python | Python Tutorial - Day #38

CodeWithHarry · 10:42 · 394,301 views · 3y ago · captions — Watch on YouTube

About this video

This video covers raising exceptions yourself with the raise statement and defining custom error types for your own code. It should show how a function can reject invalid input by raising an informative error rather than silently returning a wrong result. This maps directly onto validating engineering inputs such as negative dimensions or impossible material properties.

Why this video

It completes the topic by moving from catching errors to signaling them, which is the subtopic most relevant to writing reusable engineering functions that other code can trust.

About These Videos

These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.