Skip to content
Home » Built-in exceptions

Built-in exceptions


Built-in Exceptions in Python

Python provides a large number of built-in exception classes that represent different kinds of runtime errors.
These exceptions are automatically raised when an error occurs during program execution.

All built-in exceptions are defined in the exceptions module in Python.


What Are Built-in Exceptions? (Exam Definition)

Built-in exceptions are predefined error classes in Python that are automatically raised when certain types of errors occur at runtime, such as invalid operations, incorrect data types, indexing errors, arithmetic errors, etc.

They help in identifying and handling specific errors in programs.


Commonly Used Built-in Exceptions (Important for Exams)

Below is a list of the most important built-in exceptions along with their causes and examples.


🔹 1. SyntaxError

Occurs when Python encounters invalid syntax.

print("Hello"  # missing parenthesis

🔹 2. NameError

Occurs when a variable or function name is not defined.

print(x)   # x is not defined

🔹 3. TypeError

When an operation is applied to an incorrect data type.

print(5 + "Hello")   # int + str

🔹 4. ValueError

Occurs when a function receives the right type but wrong value.

int("abc")   # cannot convert string to int

🔹 5. ZeroDivisionError

Division or modulo by zero.

print(10 / 0)

🔹 6. IndexError

Accessing an index outside the range of a list or tuple.

a = [1,2,3]
print(a[10])

🔹 7. KeyError

When a dictionary key does not exist.

d = {"a":1}
print(d["b"])

🔹 8. AttributeError

Accessing an attribute that does not exist in an object.

x = 10
x.upper()   # int has no upper() method

🔹 9. FileNotFoundError

Trying to open a file that does not exist.

open("abc.txt")

🔹 10. ImportError / ModuleNotFoundError

Occurs when a module cannot be imported.

import mymodule  # does not exist

🔹 11. MemoryError

Python runs out of memory.


🔹 12. IndentationError

Incorrect indentation.

def func():
print("Hello")

🔹 13. RuntimeError

General error that cannot be categorized.


🔹 14. FloatingPointError

Arithmetic floating-point operation failure.


🔹 15. IOError / OSError

Input/output related issues such as reading/writing files.


🔹 16. StopIteration

Raised by iterators when no more items are available.

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

🔹 17. OverflowError

Number too large to be represented.


🔹 18. AssertionError

Occurs when an assert statement fails.

x = 5
assert x > 10   # fails

Hierarchy of Exceptions (Important for understanding)

All built-in exceptions inherit from the base class:

BaseException

  ↳ Exception
    ↳ Standard Errors like:
    - ValueError
    - TypeError
    - IndexError
    - ImportError
    - …etc
  ↳ SystemExit
  ↳ KeyboardInterrupt


Why Built-in Exceptions Are Useful?

✔ Easy identification of error source
✔ Helps in debugging
✔ Allows writing specific except blocks
✔ Improves program stability

Example:

try:
    x = int("abc")
except ValueError:
    print("Invalid value!")

Exam-Ready Summary

  • Built-in exceptions are predefined classes for handling runtime errors.
  • They cover common errors like type errors, index errors, key errors, import errors, file errors, etc.
  • They help in writing robust programs using try-except blocks.
  • Examples include TypeError, ValueError, ZeroDivisionError, FileNotFoundError.