Python/Classes & structure 8 min

Errors and testing

Fail clearly, then prove your code is right.

tryraiseassert

try/except handles a failure you expect. Catch the specific exception type, not a bare except, so real bugs still surface.

python · editable
loading editor…

Try it: Make it also accept strings with surrounding whitespace.

raise signals a problem the caller must deal with, and else/finally round out the block: else runs when nothing failed, finally always runs.

python · editable
loading editor…

A test is just a function that asserts the expected result. Writing a handful for your transformations catches regressions long before production does.

python · editable
loading editor…

practice this