Summary
The raising of an exception interrupts normal program flow and transfers control to an exception handler.
Exception handlers are defined using the
try…exceptconstruct.The
tryblocks define a context in which exceptions can be detected.Corresponding the
exceptblocks define handlers for specific types of exceptions.Python uses exceptions pervasively and many built-in language features depend on them.
The
exceptblocks can capture anexceptionobject, which is often of a standard type such asValueError,KeyErrororIndexError.Programmer errors such as
IndentationErrorandSyntaxErrorshould not normally be handled.Exceptional conditions can be signaled using the
raisekeyword which accepts a single parameter of anexceptionobject.Raise without an argument within an
exceptblock re-raises the exception which is currently being processed.We tend not to routinely check for
TypeErrors. To do so would negate the flexibility afforded to us by Python's dynamic type system.The
exception...