Challenge - 5 Problems
Custom Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of custom exception with additional attribute
What is the output of this code when the exception is caught and printed?
Python
class MyError(Exception): def __init__(self, message, code): super().__init__(message) self.code = code try: raise MyError("Oops!", 404) except MyError as e: print(f"Error: {e}, Code: {e.code}")
Attempts:
2 left
💡 Hint
Look at how the message and code are passed and accessed in the exception.
✗ Incorrect
The custom exception stores the message in the base Exception class and the code in a new attribute. Printing the exception object shows the message, and accessing e.code gives 404.
❓ Predict Output
intermediate2:00remaining
Output of overridden __str__ in custom exception
What will be printed when this code runs?
Python
class CustomError(Exception): def __init__(self, message, severity): super().__init__(message) self.severity = severity def __str__(self): return f"{self.args[0]} (Severity: {self.severity})" try: raise CustomError("Failed operation", "High") except CustomError as e: print(e)
Attempts:
2 left
💡 Hint
The __str__ method controls how the exception is printed.
✗ Incorrect
The __str__ method returns a formatted string combining the message and severity, so printing the exception shows that string.
🔧 Debug
advanced2:00remaining
Identify the error in custom exception initialization
This code tries to create a custom exception but raises an error. What is the cause?
Python
class BadError(Exception): def __init__(self, message, code): self.code = code try: raise BadError("Error happened", 500) except BadError as e: print(e)
Attempts:
2 left
💡 Hint
Check if the base Exception class is properly initialized.
✗ Incorrect
The custom exception does not call super().__init__(message), so the base Exception is not initialized, causing a TypeError when printing.
📝 Syntax
advanced2:00remaining
Syntax error in custom exception definition
Which option contains a syntax error in defining a custom exception?
Attempts:
2 left
💡 Hint
Check the class definition syntax carefully.
✗ Incorrect
Option A is missing a colon ':' after the class declaration, causing a SyntaxError.
🚀 Application
expert3:00remaining
Determine the output of nested custom exceptions
What is the output of this code snippet?
Python
class BaseError(Exception): def __init__(self, message): super().__init__(message) class DerivedError(BaseError): def __init__(self, message, code): super().__init__(message) self.code = code try: try: raise DerivedError("Failure", 123) except DerivedError as e: raise BaseError(f"Wrapped: {e}") from e except BaseError as e: print(e) print(type(e.__cause__))
Attempts:
2 left
💡 Hint
Look at how exception chaining with 'from' works and what __cause__ stores.
✗ Incorrect
The outer exception message is 'Wrapped: Failure'. The __cause__ attribute points to the original DerivedError instance, so its type is DerivedError.