Use cases for exceptions
The use cases for exceptions are very broad. We'll identify a few significant areas where exceptions are used in Python.
Some exceptions are entirely benign. The StopIteration exception is raised by an iterable that has run out of values. The for statement consumes items from the iterable until this exception is raised to signal that there's no more data. Similarly, a GeneratorExit is used when a generator is closed before producing all of its data. This is not an error; it's a signal that more data will not be requested.
Conditions that are entirely outside the program may be seen as exceptions. Unexpected OS conditions or errors are signaled by exceptions which are subclasses of the OSError exception. Some OS conditions ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access