##  4.3.  Exceptions  Â¶

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 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  ` ](simple_stmts.html#raise) statement.
Exception handlers are specified with the [ ` try  `
](compound_stmts.html#try) â¦ [ ` except  ` ](compound_stmts.html#except)
statement. The [ ` finally  ` ](compound_stmts.html#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.

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).

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  `
](../library/exceptions.html#SystemExit "SystemExit") .

Exceptions are identified by class instances. The [ ` except  `
](compound_stmts.html#except) clause is selected depending on the class of the
instance: it must reference the class of the instance or a [ non-virtual base
class  ](../glossary.html#term-abstract-base-class) thereof. The instance can
be received by the handler and can carry additional information about the
exceptional condition.

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.

See also the description of the [ ` try  ` ](compound_stmts.html#try)
statement in section [ The try statement  ](compound_stmts.html#try) and [ `
raise  ` ](simple_stmts.html#raise) statement in section [ The raise statement
](simple_stmts.html#raise) .

Footnotes

[  1  ]

This limitation occurs because the code that is executed by these operations
is not available at the time the module is compiled.

###  [ Table of Contents ](../contents.html)

  * 4\. Execution model 
    * 4.1. Structure of a program 
    * 4.2. Naming and binding 
      * 4.2.1. Binding of names 
      * 4.2.2. Resolution of names 
      * 4.2.3. Annotation scopes 
      * 4.2.4. Lazy evaluation 
      * 4.2.5. Builtins and restricted execution 
      * 4.2.6. Interaction with dynamic features 
    * 4.3. Exceptions 

####  Previous topic

[ 3\.  Data model ](datamodel.html "previous chapter")

####  Next topic

[ 5\.  The import system ](import.html "next chapter")

###  This Page

  * [ Report a Bug ](../bugs.html)
  * [ Show Source ](https://github.com/python/cpython/blob/main/Doc/reference/executionmodel.rst)

Â«

###  Navigation

  * [ index ](../genindex.html "General Index")
  * [ modules ](../py-modindex.html "Python Module Index") | 
  * [ next ](import.html "5. The import system") | 
  * [ previous ](datamodel.html "3. Data model") | 
  * ![Python logo](../_static/py.svg)
  * [ Python ](https://www.python.org/) » 
  *   *   * [ 3.12.3 Documentation ](../index.html) » 
  * [ The Python Language Reference ](index.html) » 
  * [ 4\.  Execution model ]()
  * | 
  * Theme  Auto  Light  Dark  | 

© [ Copyright ](../copyright.html) 2001-2024, Python Software Foundation.  
This page is licensed under the Python Software Foundation License Version 2.  
Examples, recipes, and other code in the documentation are addit