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 check_number(num):
    if num < 0:
        raise 'Negative number error'
    return 'Number is positive'

print(check_number(-5))
AYou cannot raise a string directly; it must be an Exception type.
BThe raise statement is missing parentheses.
CThe function should return None instead of a string.
DThe if condition should be num > 0.
Step-by-Step Solution
Solution:
  1. Step 1: Check the raise statement

    The code tries to raise a string directly, which is not allowed in Python. Only Exception types can be raised.
  2. Step 2: Correct way to raise errors

    Use raise ValueError('message') or another Exception class, not a plain string.
  3. Final Answer:

    You cannot raise a string directly; it must be an Exception type. -> Option A
  4. Quick Check:

    raise must use Exception type, not string [OK]
Quick Trick: Always raise Exception objects, not strings [OK]
Common Mistakes:
  • Raising strings instead of Exception classes
  • Forgetting to include parentheses with message
  • Changing condition incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes