Lesson 1 of 4Module 2 · 0% of course
Explore › CS, Python & AI Engineering › Python Foundations › Objects & reliability
Python Foundations · Objects & reliability

Errors, exceptions & tests

Fail loudly and on purpose: raise, catch, and prove your code works with pytest.

15 min · 5 steps · videoJump to quiz ↓
Python Tutorial: Unit Testing Your Code with the unittest Module Corey Schafer · 39 min
Open in YouTube
Step 1 of 5 · 1. try / except15 min read ·

1. try / except

from decimal import Decimal, InvalidOperation
def parse_px(s):
    try:
        return Decimal(s)
    except InvalidOperation:
        raise ValueError(f"bad price: {s!r}")

Catch the specific exception you expect. A bare except: hides real bugs, which in a recon job means silently dropping trades.

Next: 2. Tests with pytest
‹ PrevNext ›