The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python exceptions and error handling. What are the different types of exceptions in Python?
By Guest on 4th November 2022 07:33:09 AM | Syntax: PYTHON | Views: 153



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. 4.2. Naming and binding
  2. =======================
  3.  
  4. 4.3. Exceptions
  5. ===============
  6.  
  7. Exceptions are a means of breaking out of the normal flow of control
  8. of a code block in order to handle errors or other exceptional
  9. conditions.  An exception is *raised* at the point where the error is
  10. detected; it may be *handled* by the surrounding code block or by any
  11. code block that directly or indirectly invoked the code block where
  12. the error occurred.
  13.  
  14. The Python interpreter raises an exception when it detects a run-time
  15. error (such as division by zero).  A Python program can also
  16. explicitly raise an exception with the "raise" statement. Exception
  17. handlers are specified with the "try" ... "except" statement.  The
  18. "finally" clause of such a statement can be used to specify cleanup
  19. code which does not handle the exception, but is executed whether an
  20. exception occurred or not in the preceding code.
  21.  
  22. Python uses the "termination" model of error handling: an exception
  23. handler can find out what happened and continue execution at an outer
  24. level, but it cannot repair the cause of the error and retry the
  25. failing operation (except by re-entering the offending piece of code
  26. from the top).
  27.  
  28. When an exception is not handled at all, the interpreter terminates
  29. execution of the program, or returns to its interactive main loop.  In
  30. either case, it prints a stack traceback, except when the exception is
  31. "SystemExit".
  32.  
  33. Exceptions are identified by class instances.  The "except" clause is
  34. selected depending on the class of the instance: it must reference the
  35. class of the instance or a *non-virtual base class* thereof. The
  36. instance can be received by the handler and can carry additional
  37. information about the exceptional condition.
  38.  
  39. Note:
  40.  
  41.   Exception messages are not part of the Python API.  Their contents
  42.   may change from one version of Python to the next without warning
  43.   and should not be relied on by code which will run under multiple
  44.   versions of the interpreter.
  45.  
  46. See also the description of the "try" statement in section The try
  47. statement and "raise" statement in section The raise statement.
  48.  
  49. -[ Footnotes ]-
  50.  
  51. [1] This limitation occurs because the code that is executed by these
  52.     operations is not available at the time the module is compiled.
  53.  
  54.  
  55. Related questions:
  56. What are exception handling in Python?
  57. What are the different types of exceptions in Python?
  58. What are 3 basic keywords of exception handling mechanism?
  59. What is difference between error and exception in Python?
  60.  
  61.  
  62. Related video:
  63. Python Tutorial: Using Try/Except Blocks for Error Handling
  64. 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
  • Recent Pastes