Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
Python - Custom Exceptions
Consider this code:
class BaseError(Exception):
    pass

class DerivedError(BaseError):
    pass

try:
    raise DerivedError('Problem')
except BaseError as e:
    print(type(e).__name__)

What will be printed?
AError
BBaseError
CException
DDerivedError
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception inheritance and catching

    DerivedError is a subclass of BaseError, so catching BaseError catches DerivedError too.
  2. Step 2: Check the type of caught exception

    The actual exception raised is DerivedError, so type(e).__name__ is 'DerivedError'.
  3. Final Answer:

    DerivedError -> Option D
  4. Quick Check:

    Exception type reflects actual raised class [OK]
Quick Trick: Exception type shows actual raised subclass [OK]
Common Mistakes:
  • Assuming caught exception type is the base class
  • Confusing exception class names
  • Expecting generic Exception name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes