Python offers the following standard exceptions:
Exception | Description |
---|---|
BaseException | The base class for all built-in exceptions. |
SystemExit | Raised by the sys.exit() function. |
KeyboardInterrupt | Raised when the user hits the interrupt key (usually Ctrl+C or Delete). |
GeneratorExit | Raised when a generator’s close() method is called. |
Exception | The base class for all non-exiting exceptions. |
StopIteration | Raised by built-in function next() and an iterator’s __next__() method to signal that there are no further items produced by the iterator. |
StopAsyncIteration | Raised by an asynchronous iterator’s __anext__() method to stop the iteration. |
ArithmeticError | Base class for all errors related to arithmetic operations. |
FloatingPointError | Raised when a floating point operation fails. |
OverflowError | Raised when the result of an arithmetic operation is too large to be represented. |
ZeroDivisionError | Raised when division or modulo operation is performed with zero as the divisor. |
AssertionError | Raised when an assert statement fails. |
AttributeError | Raised when an attribute reference or assignment fails. |
BufferError | Raised when a buffer-related operation cannot be performed. |
EOFError | Raised when the input() function hits an end-of-file condition. |
ImportError | Raised when an import statement fails to find the module or name. |
ModuleNotFoundError | Raised by import when a module could not be found. |
LookupError | Base class for errors raised when a lookup operation fails. |
IndexError | Raised when a sequence subscript is out of range. |
KeyError | Raised when a dictionary key is not found. |
MemoryError | Raised when an operation runs out of memory. |
NameError | Raised when a local or global name is not found. |
UnboundLocalError | A subclass of NameError raised when a local variable is referenced before assignment. |
OSError | Base class for operating system-related errors. |
BlockingIOError | Raised when an operation would block on an object (e.g., socket) set for non-blocking operation. |
ChildProcessError | Raised when an operation on a child process fails. |
ConnectionError | Base class for connection-related issues. |
BrokenPipeError | Raised when a write to a pipe or socket fails because the other end is closed. |
ConnectionAbortedError | Raised when a connection attempt is aborted by the peer. |
ConnectionRefusedError | Raised when a connection attempt is refused by the peer. |
ConnectionResetError | Raised when a connection is reset by the peer. |
FileExistsError | Raised when trying to create a file or directory which already exists. |
FileNotFoundError | Raised when a file or directory is requested but cannot be found. |
InterruptedError | Raised when a system call is interrupted by a signal. |
IsADirectoryError | Raised when a file operation (e.g., open) is requested on a directory. |
NotADirectoryError | Raised when a directory operation (e.g., chdir) is requested on something which is not a directory. |
PermissionError | Raised when trying to perform an operation without the necessary permissions. |
ProcessLookupError | Raised when a given process ID does not match any running process. |
TimeoutError | Raised when a system function timed out. |
ReferenceError | Raised when a weak reference proxy is used to access a garbage collected referent. |
RuntimeError | Raised when an error is detected that doesn’t fall in any of the other categories. |
NotImplementedError | Raised by abstract methods. |
RecursionError | A subclass of RuntimeError raised when the maximum recursion depth is exceeded. |
SyntaxError | Raised when the parser encounters a syntax error. |
IndentationError | A subclass of SyntaxError raised when there is incorrect indentation. |
TabError | Raised when indentation contains inconsistent tabs and spaces. |
SystemError | Raised when the interpreter detects internal error. |
TypeError | Raised when an operation or function is applied to an object of inappropriate type. |
ValueError | Raised when a function receives an argument of the right type but inappropriate value. |
UnicodeError | Raised when a Unicode-related encoding or decoding error occurs. |
UnicodeEncodeError | Raised when a Unicode-related error occurs during encoding. |
UnicodeDecodeError | Raised when a Unicode-related error occurs during decoding. |
UnicodeTranslateError | Raised when a Unicode-related error occurs during translation. |
Warning | Base class for warning categories. |
DeprecationWarning | Raised for deprecated features. |
PendingDeprecationWarning | Raised for features that will be deprecated in the future. |
RuntimeWarning | Raised for runtime issues that do not raise errors. |
SyntaxWarning | Raised for syntax issues. |
UserWarning | Raised for user-defined warnings. |
FutureWarning | Raised for constructs that will change semantics in the future. |
ImportWarning | Raised for issues related to import . |
UnicodeWarning | Raised for Unicode-related warnings. |
BytesWarning | Raised for issues with bytes and bytearray. |
ResourceWarning | Raised for resource usage warnings. |
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