Python exception hierarchy and Its role in exception handling
Describe the hierarchy of exception classes in Python and how it helps in exception handling?
- BaseException: This is the root of the exception hierarchy. All built-in exceptions inherit from this class.
- SystemExit: This exception is raised by the sys.exit() function.
- KeyboardInterrupt: Raised when the user hits the interrupt key (normally Control-C or Delete).
- 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).
- 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.
- 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.
- 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.
- AssertionError: Raised when an assert statement fails.
- EOFError: Raised when the input() function hits an end-of-file condition (EOF) without reading any data.
- ImportError: Raised when the import statement has troubles trying to load a module.
- IOError: Raised when an I/O operation fails.
- 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.
- TypeError: Raised when an operation or function is applied to an object of inappropriate type.
- 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.
- 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.
- AttributeError: Raised when an attribute reference (see Attribute references) or assignment fails.
- 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.
- RuntimeError: Raised when an error is detected that doesn't fall in any of the other categories.
- UserWarning: Base class for warnings generated by user code.
- SyntaxError: Raised when the parser encounters a syntax error
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://www.w3resource.com/python-interview/describe-the-hierarchy-of-exception-classes-in-python-and-how-it-helps-in-exception-handling.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics