Exceptions and flow of control
Exceptions and flow of control
While it’s considered pythonic to use exceptions and to follow the rule of EAFP (“easier to ask for forgiveness than permission”), it is unwise to use exceptions for controlling the flow of program execution except within very narrow limits.
Here are some rules to follow:
Keep
try
andexcept
blocks as small as possible.Handle an exception in the most simple and direct way possible.
Avoid calling another function from within an
except
block which might send program flow away from the point where the exception was raised.
Copyright © 2023–2025 Clayton Cafiero
No generative AI was used in producing this material. This was written the old-fashioned way.