Summary

In this chapter, we've seen how we can use Python exceptions to write programs which properly handle unexpected conditions. The various kinds of exceptions reflect external conditions as well as internal conditions that may alter how our program behaves. We can use exception clauses to implement fallback processing so that our program handles these exceptional conditions gracefully.

We've also seen some things which are discouraged. The empty except clause—which matches too many kinds of exception classes—is something which is legal but should not be used.

The idea of Look Before You Leap (LBYL) programming is also generally discouraged. The Pythonic approach is summarized as Easier to Ask Forgiveness than to ask Permission (EAFP). The general ...

Get Python Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.