Overview standard exceptions

Python offers the following standard exceptions:

ExceptionDescription
BaseExceptionThe base class for all built-in exceptions.
SystemExitRaised by the sys.exit() function.
KeyboardInterruptRaised when the user hits the interrupt key (usually Ctrl+C or Delete).
GeneratorExitRaised when a generator’s close() method is called.
ExceptionThe base class for all non-exiting exceptions.
StopIterationRaised by built-in function next() and an iterator’s __next__() method to signal that there are no further items produced by the iterator.
StopAsyncIterationRaised by an asynchronous iterator’s __anext__() method to stop the iteration.
ArithmeticErrorBase class for all errors related to arithmetic operations.
FloatingPointErrorRaised when a floating point operation fails.
OverflowErrorRaised when the result of an arithmetic operation is too large to be represented.
ZeroDivisionErrorRaised when division or modulo operation is performed with zero as the divisor.
AssertionErrorRaised when an assert statement fails.
AttributeErrorRaised when an attribute reference or assignment fails.
BufferErrorRaised when a buffer-related operation cannot be performed.
EOFErrorRaised when the input() function hits an end-of-file condition.
ImportErrorRaised when an import statement fails to find the module or name.
ModuleNotFoundErrorRaised by import when a module could not be found.
LookupErrorBase class for errors raised when a lookup operation fails.
IndexErrorRaised when a sequence subscript is out of range.
KeyErrorRaised when a dictionary key is not found.
MemoryErrorRaised when an operation runs out of memory.
NameErrorRaised when a local or global name is not found.
UnboundLocalErrorA subclass of NameError raised when a local variable is referenced before assignment.
OSErrorBase class for operating system-related errors.
BlockingIOErrorRaised when an operation would block on an object (e.g., socket) set for non-blocking operation.
ChildProcessErrorRaised when an operation on a child process fails.
ConnectionErrorBase class for connection-related issues.
BrokenPipeErrorRaised when a write to a pipe or socket fails because the other end is closed.
ConnectionAbortedErrorRaised when a connection attempt is aborted by the peer.
ConnectionRefusedErrorRaised when a connection attempt is refused by the peer.
ConnectionResetErrorRaised when a connection is reset by the peer.
FileExistsErrorRaised when trying to create a file or directory which already exists.
FileNotFoundErrorRaised when a file or directory is requested but cannot be found.
InterruptedErrorRaised when a system call is interrupted by a signal.
IsADirectoryErrorRaised when a file operation (e.g., open) is requested on a directory.
NotADirectoryErrorRaised when a directory operation (e.g., chdir) is requested on something which is not a directory.
PermissionErrorRaised when trying to perform an operation without the necessary permissions.
ProcessLookupErrorRaised when a given process ID does not match any running process.
TimeoutErrorRaised when a system function timed out.
ReferenceErrorRaised when a weak reference proxy is used to access a garbage collected referent.
RuntimeErrorRaised when an error is detected that doesn’t fall in any of the other categories.
NotImplementedErrorRaised by abstract methods.
RecursionErrorA subclass of RuntimeError raised when the maximum recursion depth is exceeded.
SyntaxErrorRaised when the parser encounters a syntax error.
IndentationErrorA subclass of SyntaxError raised when there is incorrect indentation.
TabErrorRaised when indentation contains inconsistent tabs and spaces.
SystemErrorRaised when the interpreter detects internal error.
TypeErrorRaised when an operation or function is applied to an object of inappropriate type.
ValueErrorRaised when a function receives an argument of the right type but inappropriate value.
UnicodeErrorRaised when a Unicode-related encoding or decoding error occurs.
UnicodeEncodeErrorRaised when a Unicode-related error occurs during encoding.
UnicodeDecodeErrorRaised when a Unicode-related error occurs during decoding.
UnicodeTranslateErrorRaised when a Unicode-related error occurs during translation.
WarningBase class for warning categories.
DeprecationWarningRaised for deprecated features.
PendingDeprecationWarningRaised for features that will be deprecated in the future.
RuntimeWarningRaised for runtime issues that do not raise errors.
SyntaxWarningRaised for syntax issues.
UserWarningRaised for user-defined warnings.
FutureWarningRaised for constructs that will change semantics in the future.
ImportWarningRaised for issues related to import.
UnicodeWarningRaised for Unicode-related warnings.
BytesWarningRaised for issues with bytes and bytearray.
ResourceWarningRaised for resource usage warnings.

Common Built-in Exceptions (with Examples):

ValueError

Raised when a function receives an argument of the right type but an inappropriate value.

int("hello")  # Raises ValueError

TypeError

Raised when an operation is applied to an object of inappropriate type.

"5" + 3  # Raises TypeError

IndexError

Raised when trying to access an index that’s out of bounds.

my_list = [1, 2]
print(my_list[5])  # Raises IndexError

KeyError

Raised when a dictionary key is not found.

my_dict = {"name": "Alice"}
print(my_dict["age"])  # Raises KeyError

ZeroDivisionError

Occurs when you divide a number by zero.

10 / 0  # Raises ZeroDivisionError

FileNotFoundError

Raised when trying to open a file that doesn’t exist.

open("nonexistent.txt")  # Raises FileNotFoundError