liveBook · Manning

Except block

As explained in Software Engineering for Data Scientists

An except clause is a Python syntax clause that specifies code to run when a matching exception is raised in a try statement.

An except block runs when code inside a try statement raises a matching exception. It lets a workflow respond to failure instead of stopping immediately. section 3.6.3

Use an except block to:

  • Catch a specific error, such as AssertionError, and record the input that failed. section 3.6.3

Continue downloading valid tickers

for ticker in tickers: try: all_data[ticker] = si.get_data(ticker) except AssertionError: failures.append(ticker) A fake ticker is added to failures while valid tickers continue downloading.

How Software Engineering for Data Scientists covers it

How deep it goes
Demonstrates specific and broad exception matching, failure tracking, error typing, and finally-block execution.
Examples
Uses a ticker loop, yahoo_fin downloads, a fake ticker, failures collection, status messages, and recorded exception types.
What it assumes
Assumes basic Python loops, dictionaries, lists, indentation, and function calls while demonstrating error handling from working examples.
What you can do after
Apply it via Python to catch expected failures, log unsuccessful inputs, and continue a batch workflow.
How it connects
Connects try statements, exception handling, AssertionError, and finally clauses.

Also explained in

Compare how 3 books explain except block →

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.