Python built-in error and exception statements
Python contains a number of built-in exceptions, such as the known errors SyntaxError, NameError, and TypeError. It is embedded, meaning it is present in the source code at all times.
When an error occurs, Python throws a set of built-in exceptions. These built-in exceptions can be caught using the built-in local() function, which is as follows:
print(dir(locals()['__builtins__']))
The output will be:
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError',
'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError',
'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError',
'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError',
'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented',
'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError',
'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',
'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',
'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__',
'__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright',
'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input',
'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit',
'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
The following table lists the most common built-in exceptions:
Exception | Cause of Error |
AssertionError | When an assert statement fails, an error is raised. |
ArithmeticError | It specifies when an error in numerical calculations occurs. |
AttributeError | When attribute assignment or reference fails, this exception is raised. |
EOFError | When the input() method encounters a “end of file” circumstance, it throws an error (EOF) |
FloatingPointError | When a floating-point calculation fails, this error is generated. |
GeneratorExit | When the close() method of a generator is called, this variable is raised. |
ImportError | When the imported module cannot be found, this exception is thrown. |
IndexError | When the index of a sequence is out of range, this value is raised. |
KeyError | When a key is not found in a dictionary, this error is raised. |
KeyboardInterrupt | When the user pushes Ctrl+C, Ctrl+Z, or Delete, this exception is thrown. |
MemoryError | When a program runs out of memory, an error is generated. |
NameError | When a variable is not discovered in the local or global scope, this exception is raised. |
NotImplementedError | Raised through abstract methods. |
OSError | When a system operation results in a system-related error, this flag is raised. |
OverflowError | When the result of a numerical calculation is too huge, an error is raised. |
ReferenceError | This Is an Exception When a weak reference object does not exist, an error is raised. |
RuntimeError | When an error does not fit into any of the other categories, it is raised. |
StopIteration | Raised by the next() function to indicate that the iterator has no more items to return. |
SyntaxError | When a syntax problem occurs, the parser raises this exception. |
IndentationError | It raises an error when the indentation is incorrect. |
TabError | It raises an error when the indentation consists of tabs or spaces. |
SystemError | It is triggered when a system error occurs. |
SystemExit | The sys.exit() function raised this exception. |
TypeError | When a function or operation is applied to an object of the wrong type, this exception is raised. |
UnboundLocalError | When a reference to a local variable in a function or method is made but no value is bound to that variable, an exception is raised. |
UnicodeError | When a Unicode-related encoding or decoding problem occurs, this flag is raised. |
UnicodeEncodeError | When a Unicode-related problem occurs during encoding, this flag is raised. |
UnicodeDecodeError | When a Unicode-related error occurs while decoding, this flag is raised. |
UnicodeTranslateError | It defines an error. When there is an issue with a Unicode translation, this flag is raised. |
ValueError | When there is an incorrect value in a specified data type, this exception is raised. |
ZeroDivisionError | When the second operator in a division is zero, an error is raised. |
You can learn more about exceptions in Python through the following link>>
Tags
python-exception Errors and exceptions built into Python
Share page
About author
Ahmed Obaid
Hello, I am Ahmed Obaid, an Egyptian Arabic programmer. I would like to put my experiences learning Python on this site So that it will be a reference for you and me as well.
Sign up to leave a comment