Bird
Raised Fist0

What will happen when this code runs?

medium📝 Predict Output Q5 of Q15
Python - Custom Exceptions
What will happen when this code runs?
class CustomError(Exception):
    pass

def func():
    raise CustomError('Error occurred')

try:
    func()
except Exception as e:
    print('Caught:', e)
ASyntax error
BNo output
CCaught: Error occurred
DUncaught CustomError exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception raising

    func raises CustomError with message 'Error occurred'.
  2. Step 2: Check exception catching

    CustomError inherits Exception, so except Exception catches it and prints the message.
  3. Final Answer:

    Caught: Error occurred -> Option C
  4. Quick Check:

    Custom exceptions caught by Exception handler = message printed [OK]
Quick Trick: Custom exceptions inherit Exception and can be caught by it [OK]
Common Mistakes:
MISTAKES
  • Thinking CustomError is not caught by Exception
  • Expecting no output
  • Assuming syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes