1 This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled. 47 4.3. Exceptions See also the description of the$_{try}$ statement in section The try statement and$_{raise}$ statement in section The raise statement . Note: Exception messages are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter. Exceptions are identified by class instances. The$_{except}$clause is selected depending on the class of the instance: it must reference the class of the instance or a base class thereof. The instance can be received by the handler and can carry additional information about the exceptional condition. When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack traceback, except when the exception is$_{SystemExit}$. Python uses the “termination” model of error handling: an exception handler can find out what happened and continue execution at an outer level, but it cannot repair the cause of the error and retry the failing operation (except by re- entering the offending piece of code from the top). The Python interpreter raises an exception when it detects a run-time error (such as division by zero). A Python program can also explicitly raise an exception with the$_{raise}$ statement. Exception handlers are specified with the $_{try}$…$_{except}$ statement. The$_{finally}$clause of such a statement can be used to specify cleanup code which does not handle the exception, but is executed whether an exception occurred or not in the preceding code. Exceptions are a means of breaking out of the normal flow of control of a code block in order to handle errors or other exceptional conditions. An exception is raised at the point where the error is detected; it may be handled by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred. The$_{eval()}$ and$_{exec()}$ functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace. 1 The$_{exec()}$ and$_{eval()}$ functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. 4.3 Exceptions 4.2.4 Interaction with dynamic features Name resolution of free variables occurs at runtime, not at compile time. This means that the following code will print 42: i =10 def f(): print(i) i =42 f() The Python Language Reference, Release 3.9.5