ValueError
: Raised when a function receives an argument of the right type but inappropriate value.TypeError
: Raised when an operation or function is applied to an object of inappropriate type.ZeroDivisionError
: Raised when division or modulo by zero is performed.FileNotFoundError
: Raised when a file or directory is requested but cannot be found.IndexError
: Raised when a sequence subscript is out of range.KeyError
: Raised when a dictionary key is not found.AttributeError
: Raised when an invalid attribute reference is made.ImportError
: Raised when an import statement fails to find the module definition.Exception
: The base class for all built-in exceptions.RuntimeError
: Raised when an error occurs that doesn't fall into any other category.MemoryError
: Raised when an operation runs out of memory.StopIteration
: Raised to signal the end of an iterator.KeyboardInterrupt
: Raised when the user interrupts program execution, usually by pressing Ctrl+C.OverflowError
: Raised when a calculation exceeds the limits of a numeric type.NotImplementedError
: Raised when an abstract method that needs to be implemented is called.AssertionError
: Raised when an assert
statement fails.IOError
: Raised when an I/O operation fails.UnicodeError
: Raised when a Unicode-related encoding or decoding error occurs.ConnectionError
: Base class for connection-related issues.TimeoutError
: Raised when a timeout occurs during an operation.PermissionError
: Raised when trying to open a file in a way that is not allowed by the operating system.NotADirectoryError
: Raised when a file operation is attempted on a path that is not a directory.IsADirectoryError
: Raised when a file operation is attempted on a directory when a file is expected.ModuleNotFoundError
: Raised when a module cannot be found during import.RecursionError
: Raised when the maximum recursion depth is exceeded.BrokenPipeError
: Raised when a write operation is attempted on a pipe that has no readers.FileExistsError
: Raised when trying to create a file or directory that already exists.BlockingIOError
: Raised when an operation would block on a non-blocking I/O operation.ChildProcessError
: Raised when an operation on a child process fails.SystemExit
: Raised when the sys.exit()
function is called.GeneratorExit
: Raised when a generator's close()
method is called.OSError
: Raised when a system function returns a system-related error.EOFError
: Raised when the input()
function hits an end-of-file condition.try:
x = int(input("Please enter a number: "))
except ValueError:
print("That's not a valid number!")
This example attempts to convert user input into an integer. If the input is not a valid number, it raises a ValueError
, which is caught and handled by printing an error message.