MCS-201 · Unit 14

Exception Handling in Python Programming

Section 4 of 5

Block 4 · Unit 14Exception Handling in Python Programming8 min · Section 4 of 5

14.4Raising Exceptions

Covers IGNOU MCS-201 §14.4

Exceptions are not only raised by Python; your own code can raise them when it detects a situation it cannot accept, such as a negative age or an empty order. This turns silent bad data into a clear, catchable error.

The raise statement

Syntax
raise ExceptionClass("message describing the problem")
  • raise creates the exception and starts the same process as a built-in error: the rest of the block is skipped and Python looks for a handler.
  • Choose the built-in class that fits: ValueError for an unacceptable value, TypeError for a wrong type, KeyError or LookupError for something not found.
  • A bare raise inside an except block re-raises the exception being handled, after, for example, logging it.

Key takeaways

  • raise signals an error from your code.
  • Choose a fitting built-in exception and include a helpful message.
  • A raised exception is handled exactly like a built-in one.
  • A bare raise in except re-raises the current exception.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which statement raises an exception for a negative quantity?

Show answer

Answer: C. raise ValueError("negative")

Python uses raise; throw is Java/C++ syntax, and returning an exception object does not signal an error.

Question 2 · multiple choice

Which built-in exception best fits “the price must be positive”?

Show answer

Answer: A. ValueError

The type (a number) is right, but the value is unacceptable.

Question 3 · multiple choice

What does this program print?

Python · quiz_raise.py
def withdraw(balance, amount):
    if amount > balance:
        raise ValueError("insufficient funds")
    return balance - amount

try:
    print(withdraw(500, 200))
    print(withdraw(500, 900))
    print("after")
except ValueError as e:
    print("Error:", e)
Show answer

Answer: C. 300, then Error: insufficient funds

The first call succeeds; the second raises, which skips “after” and jumps to the handler.

Question 4 · multiple choice

What does a bare raise inside an except block do?

Show answer

Answer: C. Re-raises the exception currently being handled

It passes the same exception on to outer handlers, often after logging.

Question 5 · multiple choice

Which statement is true?

Show answer

Answer: B. raise can raise built-in exceptions such as ValueError

Any exception class can be raised, and raised exceptions are caught like any other.

Question 6 · short answer

Write a function set_age(age) that raises TypeError if age is not an integer and ValueError if it is outside 0 to 120, and show a call that handles both.

Show worked answer

def set_age(age): with if not isinstance(age, int): raise TypeError("age must be an integer"), if not 0 <= age <= 120: raise ValueError("age must be between 0 and 120"), and return age. Call: try: set_age(150) with except (TypeError, ValueError) as e: print("Invalid age:", e), which prints Invalid age: age must be between 0 and 120.

↑ ↓ move↵ openTab switch type