w3resource

Python exception hierarchy and Its role in exception handling

Describe the hierarchy of exception classes in Python and how it helps in exception handling?

  1. BaseException: This is the root of the exception hierarchy. All built-in exceptions inherit from this class.
  2. SystemExit: This exception is raised by the sys.exit() function.
  3. KeyboardInterrupt: Raised when the user hits the interrupt key (normally Control-C or Delete).
  4. Exception: This is the base class for most built-in exceptions. Most user-defined exceptions should inherit from this class.
    • StopIteration: Raised by iterator objects when there are no more items to return.
    • GeneratorExit: Raised when a generator or coroutine is closed; see generator.close() and coroutine.close().
    • SystemExit: This exception is raised by the sys.exit() function.
    • KeyboardInterrupt: Raised when the user hits the interrupt key (normally Control-C or Delete).
  5. StandardError (Python 2 only): In Python 2, this was the base class for all built-in exceptions. In Python 3, it was removed, and all exceptions directly inherit from the "BaseException" class.
  6. ArithmeticError: Base class for numeric errors like OverflowError and ZeroDivisionError.
    • OverflowError: Raised when the result of an arithmetic operation is too large to be represented.
    • ZeroDivisionError: Raised when the second argument of a division or modulo operation is zero. The associated value is a string indicating the type of the operands and the operation.
  7. LookupError: Base class for lookup errors.
    • IndexError: Raised when a sequence subscript is out of range.
    • KeyError: Raised when a mapping (dictionary) key is not found in the set of existing keys.
  8. AssertionError: Raised when an assert statement fails.
  9. EOFError: Raised when the input() function hits an end-of-file condition (EOF) without reading any data.
  10. ImportError: Raised when the import statement has troubles trying to load a module.
  11. IOError: Raised when an I/O operation fails.
  12. OSError: Raised for operating system-related errors.
    • FileNotFoundError: Raised when a file or directory is requested but doesn't exist.
    • PermissionError: Raised when trying to run an operation without adequate access rights - for example filesystem permissions.
  13. TypeError: Raised when an operation or function is applied to an object of inappropriate type.
  14. ValueError: Raised when an operation or function receives an argument that has the right type but an inappropriate value. The situation is not described by a more precise exception such as IndexError.
  15. NameError: Raised when a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the missing name.
  16. AttributeError: Raised when an attribute reference (see Attribute references) or assignment fails.
  17. NotImplementedError: This exception is derived from RuntimeError. In user defined base classes, abstract methods should raise this exception when they require derived classes to override the method, or while the class is being developed to indicate that the real implementation still needs to be added.
  18. RuntimeError: Raised when an error is detected that doesn't fall in any of the other categories.
  19. UserWarning: Base class for warnings generated by user code.
  20. SyntaxError: Raised when the parser encounters a syntax error


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-interview/describe-the-hierarchy-of-exception-classes-in-python-and-how-it-helps-in-exception-handling.php