Skip to content

Computer Science · Ch 1 — Exception Handling in Python

Built-in Exceptions

1.4

Built-in Exceptions

Built-in exceptions are the errors that the Python interpreter already knows about. When you write code, certain mistakes happen so often that the language designers have pre-defined a specific exception for each one. These are called built-in exceptions, and they form part of Python’s standard library.

The purpose of having these ready-made exceptions is to provide a standardised solution for common errors. When a built-in exception occurs, the interpreter automatically runs the appropriate handler code. This handler displays two things: the name of the exception that was raised, and a reason explaining what went wrong. The programmer’s job is then to read that message and take the correct action to fix the problem.

The textbook lists twelve commonly occurring built-in exceptions. Here they are, in the order the book presents them:

  1. SyntaxError – Raised when there is an error in the syntax of the Python code. This is the most basic mistake: you typed something the interpreter cannot parse at all.
  2. ValueError – Raised when a built-in method or operation receives an argument that has the right data type but an inappropriate or mismatched value. For example, passing a string like "abc" to int() gives a ValueError because the type is correct (a string) but the value cannot be converted to an integer.
  3. IOError – Raised when a file specified in a program statement cannot be opened. This could happen if the file does not exist, the path is wrong, or you lack permission.
  4. KeyboardInterrupt – Raised when the user accidentally hits the Delete or Esc key while a program is running. This interrupts the normal flow of execution.
  5. ImportError – Raised when the requested module definition is not found. For instance, if you try to import a module that is not installed or does not exist.
  6. EOFError – Raised when the input() function reaches the end of a file condition without reading any data. This typically happens when there is no more input to read.
  7. ZeroDivisionError – Raised when the denominator in a division operation is zero. Division by zero is mathematically undefined, so Python raises this exception.
  8. IndexError – Raised when an index or subscript in a sequence (like a list or string) is out of range. For example, accessing my_list[10] when the list has only five elements.
  9. NameError – Raised when a local or global variable name is not defined. This happens when you try to use a variable that has not been assigned a value yet.
  10. IndentationError – Raised due to incorrect indentation in the program code. Python relies on consistent indentation to define blocks, and any mismatch triggers this error.
  11. TypeError – Raised when an operator is supplied with a value of an incorrect data type. For example, trying to add a string and an integer with + gives a TypeError. …
Figure 1.4Example of built-in exceptions
Fig. 1.4 — Example of built-in exceptions

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

This Shell session runs three separate statements, one after another, each of which is syntactically perfectly valid Python but fails when the interpreter tries to execute it — these are runtime, or built-in, exceptions, not syntax errors.

First, print (50/0) divides 50 by zero. Python raises ZeroDivisionError: division by zero, because division by zero is mathematically undefined. Second, print (var+40) tries to use a variable var that was never assigned anywhere in this session, so Python raises NameError: name 'var' is not defined. Third, 10+'5' tries to add an integer to a string using +; Python does not automatically convert types for you, so it raises TypeError: unsupported operand type(s) for +: 'int' and 'str'. …

Table 1.1Built-in exceptions in Python
S. NoName of the Built-in ExceptionExplanation
1.SyntaxErrorIt is raised when there is an error in the syntax of the Python code.
2.ValueErrorIt is raised when a built-in method or operation receives an argument that has the right data type but mismatched or inappropriate values.
3.IOErrorIt is raised when the file specified in a program statement cannot be opened.
4KeyboardInterruptIt is raised when the user accidentally hits the Delete or Esc key while executing a program due to which the normal flow of the program is interrupted.
5ImportErrorIt is raised when the requested module definition is not found.
6EOFErrorIt is raised when the end of file condition is reached without reading any data by input().
7ZeroDivisionErrorIt is raised when the denominator in a division operation is zero.
8IndexErrorIt is raised when the index or subscript in a sequence is out of range.
9NameErrorIt is raised when a local or global variable name is not defined.
10IndentationErrorIt is raised due to incorrect indentation in the program code.