- 4.2. Naming and binding
- =======================
- 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" 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.
- 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".
- 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 *non-virtual 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" statement in section The try
- statement and "raise" statement in section The raise statement.
- -[ Footnotes ]-
- [1] This limitation occurs because the code that is executed by these
- operations is not available at the time the module is compiled.
- Related questions:
- What are exception handling in Python?
- What are the different types of exceptions in Python?
- What are 3 basic keywords of exception handling mechanism?
- What is difference between error and exception in Python?
- Related video:
- Python Tutorial: Using Try/Except Blocks for Error Handling
- https://www.youtube.com/watch?v=NIWwJbo-9_8
Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.
Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license