Python Built-in Errors: Understanding and Handling Syntax Errors

Explore common built-in errors in Python, focusing on syntax errors that occur when statements do not adhere to usage rules. Learn how the Python interpreter reports these errors and provides relevant messages to help you troubleshoot and resolve issues in your code.



Python built-in errors

The most common reason for an error in a Python program is when a statement does not follow the prescribed usage rules, resulting in a syntax error. The Python interpreter immediately reports this issue along with a relevant message.

Example:

Error Example

print "hello"
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello")?

In Python, a program may encounter errors during runtime even if no syntax errors are present. These runtime errors are known as exceptions. Python's standard library defines several built-in exceptions to handle common error scenarios.

Exception Description
AssertionError Raised when the assert statement fails.
AttributeError Raised when an attribute assignment or reference fails.
EOFError Raised when the input() function reaches the end-of-file condition.
FloatingPointError Raised when a floating point operation fails.
GeneratorExit Raised when a generator's close() method is called.
ImportError Raised when an imported module is not found.
IndexError Raised when the index of a sequence is out of range.
KeyError Raised when a key is not found in a dictionary.
KeyboardInterrupt Raised when the user hits the interrupt key (Ctrl+C or delete).
MemoryError Raised when an operation runs out of memory.
NameError Raised when a variable is not found in the local or global scope.
NotImplementedError Raised by abstract methods.
OSError Raised when a system operation causes a system-related error.
OverflowError Raised when the result of an arithmetic operation is too large to be represented.
ReferenceError Raised when a weak reference proxy is used to access a garbage collected referent.
RuntimeError Raised when an error does not fall under any other category.
StopIteration Raised by the next() function to indicate that there is no further item to be returned by the iterator.
SyntaxError Raised by the parser when a syntax error is encountered.
IndentationError Raised when there is incorrect indentation.
TabError Raised when the indentation consists of inconsistent tabs and spaces.
SystemError Raised when the interpreter detects an internal error.
SystemExit Raised by the sys.exit() function.
TypeError Raised when a function or operation is applied to an object of an incorrect type.
UnboundLocalError Raised when a reference is made to a local variable in a function or method, but no value has been bound to that variable.
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.
ValueError Raised when a function receives an argument of the correct type but improper value.
ZeroDivisionError Raised when the second operand of a division or module operation is zero.

Examples:

IndexError: This error occurs when trying to access an item at an invalid index.

Example

L1=[1, 2, 3]
L1[3]
IndexError: list index out of range

ModuleNotFoundError: This error is raised when a required module cannot be found.

Example

import notamodule
ModuleNotFoundError: No module named 'notamodule'

KeyError: Raised when a key is not found in a dictionary.

Example

D1={'1':"aa", '2':"bb", '3':"cc"}
D1['4']
KeyError: '4'

ImportError: Raised when a specified function cannot be found in an imported module.

Example

from math import cube
ImportError: cannot import name 'cube'

StopIteration: Raised when the next() function goes beyond the iterator items.

Example

it=iter([1, 2, 3])
next(it)
next(it)
next(it)
next(it)
StopIteration

TypeError: Raised when an operation or function is applied to an object of an inappropriate type.

Example

'2'+2
TypeError: must be str, not int

ValueError: Raised when a function receives an argument of the correct type but improper value.

Example

int('xyz')
ValueError: invalid literal for int() with base 10: 'xyz'

NameError: Raised when an object could not be found.

Example

age
NameError: name 'age' is not defined

ZeroDivisionError: Raised when the second operand of a division or modulo operation is zero.

Example

x=100/0
ZeroDivisionError: division by zero

KeyboardInterrupt: Raised when the user interrupts program execution (Ctrl+C or delete).

Example

name=input('enter your name')
(user interrupts input)