Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
Python - Custom Exceptions
What will be the output of this code?
class MyError(Exception):
    pass

def test(value):
    if value < 0:
        raise MyError("Negative value")
    return value

try:
    print(test(-1))
except MyError as e:
    print(e)
ANegative value
B-1
CNone
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze function behavior

    The function test raises MyError with message "Negative value" if input is less than 0.
  2. Step 2: Trace try-except block

    Calling test(-1) raises MyError. The except block catches it and prints the error message.
  3. Final Answer:

    Negative value -> Option A
  4. Quick Check:

    Raised custom exception message printed [OK]
Quick Trick: Raised custom exception prints its message in except block [OK]
Common Mistakes:
  • Expecting function to return -1
  • Thinking no output occurs
  • Confusing exception name with message

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes