Python Exceptions
Chris Roda · 2024
Special events are any type of situation that occurs during program execution requiring some sort of script interactivity. Many are harmless, but others create script instability. Users should never have to deal with program interruptions and crashes. When situations occur requiring program interactivity, attention to them is made using the raise command. The raise command informs Python that a special event, called an exception, has occurred and requires attention. The try-except protocol addresses exceptions. Python enters a heightened state of alertness when the code block after a try command is executed. When an exception occurs during execution, Python attempts to find the specific code block defined by the exception type. After successful discovery, Python executes the code block, and the exception is appropriately addressed. The else component of the protocol provides instructions when no exception is encountered. The finally component provides code that is executed regardless of exception occurrence. A hierarchy of built-in exceptions is provided. However, when custom exceptions are required, programmers may derive new exception types from existing built-in types. The traceback module is helpful to capturing and recording exception behavior.