Understanding Python's Pre-Constructed Exceptions for Beginners
Maximizing Python's Built-in Exceptions for Better Code

Explore the Most Common Built-In Errors in Python β Complete with Code Examples! Python is a universal language with many powerful features, but it can also throw various errors when your code encounters an issue. Here is an overview of some of the most common built-in errors in Python, complete with code examples to illustrate how they can occur.
- SyntaxError: This error is raised when the Python interpreter encounters invalid syntax in your code. For example, if you forget to close a parenthesis, you may see a SyntaxError.
# Example of a SyntaxError
def greet(name):
print("Hello, " + name # missing parenthesis mark at the end
greet("John")
- TypeError: This error is raised when you try to operate on an object of an incorrect type. For example, if you try to add a string and an integer, you will get a TypeError.
# Example of a TypeError
a = "5"
b = 10
print(a + b) # cannot concatenate a string and an int
------------------------------------------------------
TypeError: can only concatenate str (not "int") to str
- NameError: This error is raised when you try to access a variable that has not been defined. For example, if you try to use a variable you forgot to assign a value to, you will get a NameError.
# Example of a NameError
print(x) # x has not been defined
----------------------------------
NameError: name 'x' is not defined
- IndexError: This error is raised when you try to access a list or string using an out-of-bounds index. For example, if you try to access the fifth element of a list with only three parts, you will get an IndexError.
# Example of an IndexError
numbers = [1, 2, 3]
print(numbers[4]) # index 4 is out of bounds for the list
----------------------------------------------------------
IndexError: list index out of range
- KeyError: This error is raised when you try to access a dictionary using a key that does not exist in the dictionary. For example, if you try to access a value using a key that has not been added to the dictionary, you will get a KeyError.
# Example of a KeyError
colors = {"red": "#ff0000", "green": "#00ff00"}
print(colors["blue"]) # "blue" is not a key in the dictionary
--------------------------------------------------------------
KeyError: 'blue'
- ValueError: This error is raised when you pass a value of the wrong type to a function or method. For example, if you try to convert a string to an integer using the
int()function and the string cannot be parsed as an integer; you will get a ValueError.
# Example of a ValueError
age = "twenty"
int(age) # cannot convert the string "twenty" to an int
------------------------------------------------------------
ValueError: invalid literal for int() with base 10: 'twenty'
Understanding and handling Python's built-in errors can help you write more reliable and robust programs. Learn about common Python errors and how to debug them for better coding.
To get the complete list of Exceptions, check out the documentation, Built-in Exceptions β Python 3.11.1 documentation. The hierarchy of the built-in exceptions:
BaseException
βββ BaseExceptionGroup
βββ GeneratorExit
βββ KeyboardInterrupt
βββ SystemExit
βββ Exception
βββ ArithmeticError
β βββ FloatingPointError
β βββ OverflowError
β βββ ZeroDivisionError
βββ AssertionError
βββ AttributeError
βββ BufferError
βββ EOFError
βββ ExceptionGroup [BaseExceptionGroup]
βββ ImportError
β βββ ModuleNotFoundError
βββ LookupError
β βββ IndexError
β βββ KeyError
βββ MemoryError
βββ NameError
β βββ UnboundLocalError
βββ OSError
β βββ BlockingIOError
β βββ ChildProcessError
β βββ ConnectionError
β β βββ BrokenPipeError
β β βββ ConnectionAbortedError
β β βββ ConnectionRefusedError
β β βββ ConnectionResetError
β βββ FileExistsError
β βββ FileNotFoundError
β βββ InterruptedError
β βββ IsADirectoryError
β βββ NotADirectoryError
β βββ PermissionError
β βββ ProcessLookupError
β βββ TimeoutError
βββ ReferenceError
βββ RuntimeError
β βββ NotImplementedError
β βββ RecursionError
βββ StopAsyncIteration
βββ StopIteration
βββ SyntaxError
β βββ IndentationError
β βββ TabError
βββ SystemError
βββ TypeError
βββ ValueError
β βββ UnicodeError
β βββ UnicodeDecodeError
β βββ UnicodeEncodeError
β βββ UnicodeTranslateError
βββ Warning
βββ BytesWarning
βββ DeprecationWarning
βββ EncodingWarning
βββ FutureWarning
βββ ImportWarning
βββ PendingDeprecationWarning
βββ ResourceWarning
βββ RuntimeWarning
βββ SyntaxWarning
βββ UnicodeWarning
βββ UserWarning




