Python Built-in Exceptions
Overview
In this tutorial, we will look at some Python built-in exceptions. Python has plenty of built-in exceptions that are raised when corresponding errors occur. We can view all the built-in exceptions using the built-in local() function as follows:
Python Built-in Exceptions
Some of the common built-in exceptions in Python are as follows. These are also called Python standard exceptions.
Exception |
Cause of Error |
Assertion Error | This exception is raised when an assert statement fails |
AttributeError | This exception is raised when attribute assignment or reference fails |
EOFError | This exception is raised when the input() function reaches the end-of-file condition. |
FloatingPointError | The FloatingPointError exception is raised when a floating point operation fails. |
FileNotFoundError | The FileNotFoundError exception is raised when the program tries to open a file that does not exist on the file system. |
GeneratorExit | This exception is raised when a generator’s close() method is called. |
ImportError | This exception is raised when the imported module is not found. We get this error when we try to import a module that does not exist. |
IndexError | This is raised when the index of a sequence is out of range. |
IndentationError | This IndentationError exception is raised when there is an incorrect indentation in the code, usually when mixing tabs and spaces or inconsistent indentation. |
KeyError | KeyError exception is raised when a key is not found in a dictionary. |
KeyboardInterrupt | This exception is raised when the user hits the interrupt key(Ctrl+C or Delete) |
MemoryError | MemoryError is raised when an operation runs out of memory. |
NameError | The NameError exception is raised when a variable is not found in the program’s local or global scope. |
Many others include OSError, OverflowError, RuntimeError, SyntaxError, SystemError, ZeroDivisionError, etc.
—
Python Tutorials
Python Tutorial on this website can be found at:
More information on Python is available at the official website: