0
0
Pythonprogramming~20 mins

Custom error messages in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
AAge must be at least 18
BAccess granted
CValueError
DTypeError
Attempts:
2 left
💡 Hint
The function raises an error if age is less than 18, and the error message is printed.
Predict Output
intermediate
2: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)
ACannot divide by zero!
BNone
CZeroDivisionError
Ddivision by zero
Attempts:
2 left
💡 Hint
The custom message inside the error is printed by the except block.
Predict Output
advanced
2: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}")
AMyError
BError: Something went wrong!
CException
DSomething went wrong!
Attempts:
2 left
💡 Hint
The custom exception message is passed to the print statement with a prefix.
Predict Output
advanced
2: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)
ANegative value not allowed
BZeroDivisionError
CValueError
DZero is not allowed
Attempts:
2 left
💡 Hint
The input is zero, so the ZeroDivisionError is raised and caught.
🧠 Conceptual
expert
3: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.
A
class CustomError(Exception):
    message = "Default error message"
    def __init__(self, message):
        self.message = message
B
class CustomError(Exception):
    def __init__(self):
        self.message = "Default error message"
    def __str__(self):
        return self.message
C
class CustomError(Exception):
    def __init__(self, message="Default error message"):
        super().__init__(message)
D
class CustomError(Exception):
    def __init__(self, message):
        print(message)
Attempts:
2 left
💡 Hint
The correct class passes the message to the base Exception class and allows a default.