Python Built-in Exceptions
Note: This article is for my own reference.
In Python, exceptions are represented by classes. Every exception that can be raised in Python is an instance of a class that is derived from the BaseException
class. This hierarchy allows for structured and organized handling of different types of errors or exceptional situations that may occur during program execution.
-
BaseException:
BaseException
is the base class for all built-in exceptions in Python. This means that all other exception classes are derived from it, forming an inheritance hierarchy. -
Inheritance and Exception Hierarchy: When you use a
try
statement with anexcept
clause that specifies a particular exception class, that clause can handle not only instances of that specific exception class but also instances of any exception classes that inherit from it. For instance, if you catchException
in anexcept
clause, it will catch any exception derived fromException
. -
Handling Derived Exceptions: If you have a custom exception class that you've defined, and it's derived from a built-in exception class, you can catch instances of your custom exception class using an
except
clause that mentions the base built-in exception class. -
Exception Equivalence: The explanation also highlights that two exception classes, even if they have the same name, are not equivalent unless they share a subclass relationship. This means that even if you have two different exception classes with the same name, they are distinct and are not treated as equivalent by the exception handling mechanism.
Exception Name | Description |
---|---|
BaseException | Base class for all built-in exceptions |
SystemExit | Raised when sys.exit() is called |
KeyboardInterrupt | Raised when user interrupts program execution (Ctrl+C) |
Exception | Base class for non-system-exiting exceptions |
StopIteration | Raised to signal the end of an iterator |
GeneratorExit | Raised when a generator's close() method is called |
ArithmeticError | Base class for arithmetic errors |
ZeroDivisionError | Raised when division or modulo by zero |
ValueError | Raised when a function receives an inappropriate value |
KeyError | Raised when a dictionary key is not found |
IndexError | Raised when an index is not found in a sequence |
FileNotFoundError | Raised when a file or directory is not found |
IOError | Raised for I/O-related errors |
ImportError | Raised when an import statement fails |
ModuleNotFoundError | Raised when a module is not found |
NameError | Raised when a local or global name is not found |
AttributeError | Raised when an attribute reference fails |
TypeError | Raised when an operation or function is applied to an inappropriate type |
RuntimeError | Base class for runtime errors |
MemoryError | Raised when an operation runs out of memory |
NotImplementedError | Raised when an abstract method is not implemented |
SyntaxError | Raised for syntax errors in the code |
IndentationError | Raised for incorrect indentation |
TabError | Raised for inconsistent mixing of tabs and spaces |
OSError | Base class for operating system-related errors |
PermissionError | Raised when insufficient permissions are encountered |
FileExistsError | Raised when a file already exists |
TimeoutError | Raised when an operation times out |
ConnectionError | Base class for connection-related errors |
ConnectionRefusedError | Raised when connection is refused |
ConnectionAbortedError | Raised when connection is aborted |
ConnectionResetError | Raised when connection is reset |
Please note that this is not an exhaustive list, and there are more exceptions available in Python. Also, the availability of exceptions may vary depending on the Python version you are using.
In summary, this concept of exception hierarchy and inheritance allows for flexible and organized handling of exceptions in Python, enabling you to catch specific types of errors or exceptions while also providing a mechanism to catch more general types of exceptions.
Post Tags: