Bird
0
0

What will be printed when the following code is executed?

medium📝 Predict Output Q4 of 15
Python - Custom Exceptions
What will be printed when the following code is executed?
class CustomException(Exception):
    pass

try:
    raise CustomException('Error occurred')
except CustomException as err:
    print(err)
AError occurred
BCustomException
CNone
DTraceback error
Step-by-Step Solution
Solution:
  1. Step 1: Understand the exception raising

    The code raises CustomException with the message 'Error occurred'.
  2. Step 2: Catching the exception

    The exception is caught as err in the except block.
  3. Step 3: Printing the exception object

    Printing err outputs the message passed during raising, which is 'Error occurred'.
  4. Final Answer:

    Error occurred -> Option A
  5. Quick Check:

    Exception message prints when caught and printed [OK]
Quick Trick: Printing exception instance shows its message [OK]
Common Mistakes:
  • Assuming printing exception prints class name
  • Expecting no output or None
  • Confusing exception object with traceback

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes