Summary
In this chapter, we looked at exceptions and context managers.
We saw that exceptions are Python’s way of signaling that an error has occurred. We showed you how to catch exceptions so that your program does not fail when errors inevitably do happen.
We also showed you how you can raise exceptions yourself when your own code detects an error, and that you can even define your own exception types. We saw exception groups and the new syntax that extends the except
clause. We ended our exploration of exceptions by seeing that they are not only useful for signaling errors but can also be used as a flow-control mechanism.
We ended the chapter with a brief overview of context managers. We saw how to use the with
statement to enter a context defined by a context manager that performs cleanup operations when we exit the context. We also showed you how to create your own context managers, either as part of a class or by using a generator function.
We will see...