This chapter rounds out Part VII, with a collection of exception design topics and examples, followed by this part's gotchas and exercises. Because this chapter also closes out the core language material of this book, it also includes a brief overview of development tools, by way of migration to the rest of this book.
Our examples so far have used only a single
catch exceptions, but what happens if one
inside another? For that matter, what does it mean if a
try calls a function that runs another
statements can nest in terms of both syntax, and
the runtime control flow through your code.
Both these cases can be understood if you realize that Python
try statements at runtime. When an exception is
raised, Python returns to the most recently entered
try statement with a matching
except clause. Since each
statement leaves a marker, Python can jump back to earlier
trys by inspecting the markers stacked. This
nesting of active handlers is what we mean by
try statements entered earlier in
the program's execution flow.
For example, Figure 26-1 illustrates what occurs
except statements nest
at runtime. Because the amount of code that can go into a
try clause block can be substantial (e.g., function calls), it will typically invoke other code that may be watching for the same exception. When the exception is eventually raised, Python jumps ...