Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q14 of 15
Python - Custom Exceptions
Identify the error in this code snippet:
class MyError(Exception):
    pass

try:
    raise MyError
except MyError:
    print("Caught error")
AIncorrect exception name in except block
BNo error, code runs fine
CSyntax error in class definition
DMissing parentheses when raising MyError
Step-by-Step Solution
Solution:
  1. Step 1: Check how the exception is raised

    In Python, it is valid to raise an exception class without parentheses if it has no __init__ arguments.
  2. Step 2: Identify the problem in the code

    The code uses 'raise MyError' without parentheses, which is valid and does not raise an error.
  3. Final Answer:

    No error, code runs fine -> Option B
  4. Quick Check:

    Raising exception class without parentheses is allowed [OK]
Quick Trick: Raising exception class without parentheses is valid if no arguments [OK]
Common Mistakes:
  • Omitting parentheses after exception name
  • Mismatching exception names in except block
  • Incorrect class syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes