Skip to content
Exercises · Q2

Q.When are the following built-in exceptions raised? Give examples to support your answers.

a) ImportError
b) IOError
c) NameError
d) ZeroDivisionError
Puducherry CbseNCERTSubjective· 3mImportance★★★★★
22% · 2/9 Questions
✓ Free question

This explanation details when four common built-in Python exceptions — ImportError, IOError, NameError, and ZeroDivisionError — are raised, providing a conceptual understanding and practical code examples for each.

Let's explore when each of these built-in Python exceptions is raised, along with supporting examples.

a) ImportError

An ImportError is raised when an import statement fails to find or load a module. This can occur for several reasons:

  • The module name is misspelled.
  • The module does not exist in any of the directories Python searches (the sys.path).
  • There's a circular import, or an error within the module itself that prevents it from being successfully imported.

Example:

Let's try to import a module that doesn't exist.

import non_existent_module

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'non_existent_module'

Explanation:

The Python interpreter attempts to locate and load a module named non_existent_module. Since no such module is found in the standard library or the current Python path, an ImportError is raised, indicating that the module could not be imported.

b) IOError

An IOError (Input/Output Error) is raised when an input/output operation fails. This typically involves file operations, but can also relate to other I/O streams. In modern Python, IOError is often an alias for OSError, and more specific exceptions like FileNotFoundError (a subclass of OSError) are commonly raised for file-related issues. However, the underlying concept of an I/O failure remains.

Example:

Let's try to open a file for reading that does not exist.

try:
    with open("non_existent_file.txt", "r") as f:
        content = f.read()
except IOError as e:
    print(f"Caught an IOError: {e}")

Output:

Caught an IOError: [Errno 2] No such file or directory: 'non_existent_file.txt'

Explanation:

When open("non_existent_file.txt", "r") is executed, the operating system is asked to provide a file handle for non_existent_file.txt in read mode. Since no such file exists, the operating system reports an error, which Python catches and raises as a FileNotFoundError (which is a subclass of OSError, and thus can be caught by IOError for backward compatibility). The try...except block demonstrates how you would handle such an error gracefully.

c) NameError

A NameError is raised when a local or global name is not found. This means you've tried to use a variable, function, or object by a name that hasn't been defined or is not accessible in the current scope.

Example:

Let's try to use a variable before it has been assigned a value.

print(undefined_variable)

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'undefined_variable' is not defined

Explanation:

The print() function attempts to access the value associated with the name undefined_variable. Since undefined_variable has not been assigned any value or defined anywhere in the program's scope, Python cannot find a corresponding object for that name, leading to a NameError.

d) ZeroDivisionError

A ZeroDivisionError is raised when the second operand of a division or modulo operation is zero. Division by zero is mathematically undefined, and Python prevents such operations to maintain program integrity.

Example:

Let's attempt to divide an integer by zero.

result = 10 / 0

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

Explanation:

The expression 10 / 0 attempts to perform division where the divisor is zero. Python detects this invalid mathematical operation and immediately raises a ZeroDivisionError to prevent the program from proceeding with an undefined result.

✓Final answer

The explanations and examples above detail when ImportError, IOError, NameError, and ZeroDivisionError are raised in Python.

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.