liveBook · Manning

Finally

As explained in Software Engineering for Data Scientists

A finally statement is Python syntax that specifies code to run after a try statement regardless of whether an exception occurred.

A finally clause runs its code after a try block, whether the protected code succeeds or raises an exception. With try, except, and finally together, it supports predictable work inside data-download loops. section 3.6.3

Always report each ticker

When downloading stock data, try stores successful results, except records failed tickers, and finally prints every ticker, including one that caused an error.

How Software Engineering for Data Scientists covers it

How deep it goes
Explains execution order through try, except, and finally, but does not develop broader resource-cleanup patterns.
Examples
Demonstrates finally in a loop downloading stock prices for valid and invalid ticker symbols.
What it assumes
Assumes familiarity with try and except while introducing finally as an additional exception-handling clause.
What you can do after
Apply it via Python syntax to guarantee a selected action runs after attempted code.
How it connects
Connects finally with try statements, except clauses, exceptions, and error handling.

Also explained in

Compare how 4 books explain finally →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Engineering for Data Scientists. The section links point to the parts of the book it draws on.