Challenge - 5 Problems
Custom Exception Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of custom exception handling
What will be the output of this Python code that defines and uses a custom exception?
Python
class MyError(Exception): pass def test(): try: raise MyError("Oops") except MyError as e: print(f"Caught: {e}") try: test() except Exception: print("General exception caught")
Attempts:
2 left
💡 Hint
Look at which exception is raised and which is caught inside the test function.
✗ Incorrect
The custom exception MyError is raised and caught inside the test function's try-except block, so it prints 'Caught: Oops'. The outer try-except does not run its except block.
🧠 Conceptual
intermediate1:30remaining
Why inherit from Exception for custom exceptions?
Why is it recommended to inherit custom exceptions from the built-in Exception class in Python?
Attempts:
2 left
💡 Hint
Think about how Python's try-except blocks catch exceptions.
✗ Incorrect
Inheriting from Exception ensures that the custom exception behaves like standard exceptions and can be caught by except Exception blocks.
❓ Predict Output
advanced2:00remaining
Output of custom exception with additional attributes
What is the output of this code that raises a custom exception with extra data?
Python
class ValidationError(Exception): def __init__(self, message, code): super().__init__(message) self.code = code try: raise ValidationError("Invalid input", 400) except ValidationError as e: print(f"Error: {e}, Code: {e.code}")
Attempts:
2 left
💡 Hint
Check how the message and code are passed and accessed.
✗ Incorrect
The message is passed to the base Exception class and stored as the exception message. The code attribute is stored separately and accessed correctly.
🔧 Debug
advanced1:30remaining
Identify the error in this custom exception definition
What error will this code raise when defining a custom exception?
Python
class MyException: def __init__(self, message): self.message = message raise MyException("Error happened")
Attempts:
2 left
💡 Hint
Check what Python requires for something to be raised as an exception.
✗ Incorrect
Python requires exceptions to inherit from BaseException or its subclasses. This class does not inherit from Exception, so raising it causes a TypeError.
🚀 Application
expert2:00remaining
How many items in the dictionary of custom exceptions?
Given this code, how many keys are in the dictionary 'errors' after execution?
Python
class ErrorA(Exception): pass class ErrorB(Exception): pass class ErrorC(ErrorA): pass errors = {} for err in [ErrorA, ErrorB, ErrorC]: errors[err.__name__] = err errors['ErrorA'] = 'Custom message'
Attempts:
2 left
💡 Hint
Consider how dictionary keys are assigned and overwritten.
✗ Incorrect
The dictionary initially has 3 keys: 'ErrorA', 'ErrorB', 'ErrorC'. The line errors['ErrorA'] = 'Custom message' replaces the value for 'ErrorA' but does not remove the key. So total keys remain 3.