0
0
Pythonprogramming~20 mins

Raising exceptions in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Mastery
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 exception raising code?

Consider the following Python code that raises an exception. What will be printed when this code runs?

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(f"Error: {e}")
AAccess granted
BNo output
CValueError: Age must be at least 18
DError: Age must be at least 18
Attempts:
2 left
💡 Hint

Think about what happens when raise is called inside the function and how the try-except block handles it.

Predict Output
intermediate
2:00remaining
What error does this code raise?

Look at this code snippet. What error will it raise when executed?

Python
def divide(x, y):
    if y == 0:
        raise ZeroDivisionError("Cannot divide by zero")
    return x / y

result = divide(10, 0)
AZeroDivisionError: Cannot divide by zero
BValueError: Cannot divide by zero
CTypeError: unsupported operand type(s)
DNo error, returns 0
Attempts:
2 left
💡 Hint

Check the condition that triggers the raise statement and the type of exception raised.

Predict Output
advanced
2:00remaining
What is the output of this nested exception raising code?

Analyze this code that raises exceptions inside nested try-except blocks. What will be printed?

Python
def func():
    try:
        raise KeyError("Key missing")
    except KeyError:
        raise ValueError("Value error raised")

try:
    func()
except ValueError as e:
    print(f"Caught: {e}")
ACaught: Value error raised
BNo output
CKeyError: Key missing
DCaught: Key missing
Attempts:
2 left
💡 Hint

Notice the exception raised inside the except block and which exception is caught outside.

Predict Output
advanced
2:00remaining
What happens when this code raises an exception without a message?

What will be the output of this code that raises an exception without a message?

Python
try:
    raise RuntimeError
except RuntimeError as e:
    print(f"Caught an error: {e}")
ANo output
BCaught an error:
CRuntimeError
DCaught an error: RuntimeError
Attempts:
2 left
💡 Hint

Check what the exception object e contains when no message is provided.

🧠 Conceptual
expert
3:00remaining
Which option correctly raises a custom exception with a message?

You want to create and raise a custom exception MyError with the message "Something went wrong". Which option does this correctly?

A
class MyError(Exception):
    pass

raise MyError
B
class MyError(Exception):
    def __init__(self):
        super().__init__("Something went wrong")

raise MyError()
C
class MyError(Exception): pass

raise MyError("Something went wrong")
D
class MyError(Exception):
    def __init__(self, msg):
        self.msg = msg

raise MyError("Something went wrong")
Attempts:
2 left
💡 Hint

Remember how to define a custom exception and pass a message when raising it.