Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q14 of 15
Python - Advanced Exception Handling
Find the error in this code snippet:
def divide(a, b):
    if b == 0:
        raise "Cannot divide by zero"
    return a / b

print(divide(10, 0))
ANo error, code runs fine
Braise must be followed by an exception instance, not a string
CDivision by zero is allowed in Python
DFunction divide should return None when b is zero
Step-by-Step Solution
Solution:
  1. Step 1: Identify the raise statement usage

    The code uses raise "Cannot divide by zero", which raises a string, not an exception object.
  2. Step 2: Understand correct raise syntax

    Python requires raising an exception instance, e.g., raise ValueError("Cannot divide by zero").
  3. Final Answer:

    raise must be followed by an exception instance, not a string -> Option B
  4. Quick Check:

    raise needs exception object, not string [OK]
Quick Trick: Always raise an exception object, not a string [OK]
Common Mistakes:
  • Raising strings instead of exceptions
  • Ignoring that division by zero causes error
  • Assuming code runs without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes