Python Built-in Exceptions
Python Built-in Exceptions
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:
Some of the common built-in exceptions in Python are as follows. These are also called Python standard exceptions.
| Exception |
Description |
| AssertionError | This exception is raised when an assert statement fails in the test. |
| 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. |
| 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 invoked. |
| 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 on the keyboard. (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.
| OSError | OSError is raised when system operation causes system-related error. |
| OverflowError | OverflowError is raised when the result of an arithmetic operation is too large to be represented. |
| ReferenceError | ReferenceError is raised when a weak reference proxy is used to access a garbage-collected referent. |
| RuntimeError | RuntimeError is raised when an error does not fall under any other category. |
| StopIteration | The next() function raises StopIteration to indicate that the iterator will no longer return any items. |
| SyntaxError | SyntaxError is raised by the parser when a syntax error is encountered. |
| SystemError | SystemError is raised when the interpreter detects an internal error. |
| SystemExit | SystemExit is raised by the sys.exit() function. |
| TypeError | TypeError is raised when a function or operation is applied to an object of incorrect type. |
| UnicodeError | UnicodeError is raised when a Unicode-related encoding or decoding error occurs. |
| ValueError | ValueError is raised when a function gets an argument of correct type but improper value. |
| ZeroDivision Error | ZeroDivisionError is raised when the second operand of division or modulo operation is zero. |
Example
Example of Python Built-in exceptions:
self.assertEqual(“12 + 7”,119)
AssertionError: ’12 + 7′ != 119

—
Python Tutorials
Python Tutorial on this website can be found at: