Challenge - 5 Problems
Custom Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code with a custom error message?
Look at the code below. What will it print when run?
Python
def check_age(age): if age < 18: raise ValueError("Age must be at least 18") return "Access granted" try: print(check_age(16)) except ValueError as e: print(e)
Attempts:
2 left
💡 Hint
The function raises an error if age is less than 18, and the error message is printed.
✗ Incorrect
The function raises a ValueError with the message 'Age must be at least 18' when age is 16. The except block catches this and prints the message.
❓ Predict Output
intermediate2:00remaining
What error message is shown here?
What will this code print when run?
Python
def divide(a, b): if b == 0: raise ZeroDivisionError("Cannot divide by zero!") return a / b try: print(divide(10, 0)) except ZeroDivisionError as e: print(e)
Attempts:
2 left
💡 Hint
The custom message inside the error is printed by the except block.
✗ Incorrect
The function raises a ZeroDivisionError with the message 'Cannot divide by zero!' which is caught and printed.
❓ Predict Output
advanced2:00remaining
What is the output of this code with a custom exception class?
What will this code print when run?
Python
class MyError(Exception): def __init__(self, message): super().__init__(message) try: raise MyError("Something went wrong!") except MyError as e: print(f"Error: {e}")
Attempts:
2 left
💡 Hint
The custom exception message is passed to the print statement with a prefix.
✗ Incorrect
The custom exception MyError is raised with the message 'Something went wrong!'. The except block prints 'Error: ' plus the message.
❓ Predict Output
advanced2:00remaining
What error message is shown here with multiple exceptions?
What will this code print when run?
Python
def check_value(x): if x < 0: raise ValueError("Negative value not allowed") elif x == 0: raise ZeroDivisionError("Zero is not allowed") return x try: check_value(0) except ValueError as e: print(e) except ZeroDivisionError as e: print(e)
Attempts:
2 left
💡 Hint
The input is zero, so the ZeroDivisionError is raised and caught.
✗ Incorrect
The function raises ZeroDivisionError with message 'Zero is not allowed' for input 0. The except block for ZeroDivisionError prints the message.
🧠 Conceptual
expert3:00remaining
Which option correctly defines a custom exception with a default message?
Choose the code that defines a custom exception class with a default error message that can be overridden.
Attempts:
2 left
💡 Hint
The correct class passes the message to the base Exception class and allows a default.
✗ Incorrect
Option C correctly defines __init__ with a default message and calls super().__init__ to set it. This allows overriding the message or using the default.