Skip to main content

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.

  1. 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.

  2. Inheritance and Exception Hierarchy: When you use a try statement with an except 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 catch Exception in an except clause, it will catch any exception derived from Exception.

  3. 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.

  4. 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 NameDescription
BaseExceptionBase class for all built-in exceptions
SystemExitRaised when sys.exit() is called
KeyboardInterruptRaised when user interrupts program execution (Ctrl+C)
ExceptionBase class for non-system-exiting exceptions
StopIterationRaised to signal the end of an iterator
GeneratorExitRaised when a generator's close() method is called
ArithmeticErrorBase class for arithmetic errors
ZeroDivisionErrorRaised when division or modulo by zero
ValueErrorRaised when a function receives an inappropriate value
KeyErrorRaised when a dictionary key is not found
IndexErrorRaised when an index is not found in a sequence
FileNotFoundErrorRaised when a file or directory is not found
IOErrorRaised for I/O-related errors
ImportErrorRaised when an import statement fails
ModuleNotFoundErrorRaised when a module is not found
NameErrorRaised when a local or global name is not found
AttributeErrorRaised when an attribute reference fails
TypeErrorRaised when an operation or function is applied to an inappropriate type
RuntimeErrorBase class for runtime errors
MemoryErrorRaised when an operation runs out of memory
NotImplementedErrorRaised when an abstract method is not implemented
SyntaxErrorRaised for syntax errors in the code
IndentationErrorRaised for incorrect indentation
TabErrorRaised for inconsistent mixing of tabs and spaces
OSErrorBase class for operating system-related errors
PermissionErrorRaised when insufficient permissions are encountered
FileExistsErrorRaised when a file already exists
TimeoutErrorRaised when an operation times out
ConnectionErrorBase class for connection-related errors
ConnectionRefusedErrorRaised when connection is refused
ConnectionAbortedErrorRaised when connection is aborted
ConnectionResetErrorRaised 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: