Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does it mean to raise an exception in Python?
Raising an exception means telling Python that an error or unexpected situation happened. It stops normal code flow and looks for a way to handle the problem.
Click to reveal answer
beginner
How do you raise a ValueError with a custom message in Python?
Use the raise keyword followed by the exception type and message, like this:
raise ValueError("This is a custom error message")
Click to reveal answer
beginner
What happens if you raise an exception but do not handle it?
If an exception is raised and not caught by any try-except block, the program stops running and shows an error message.
Click to reveal answer
intermediate
Can you raise your own custom exception class in Python?
Yes! You can create a new class that inherits from <code>Exception</code> and then raise it like any other exception.
Click to reveal answer
beginner
What is the purpose of raising exceptions in your code?
Raising exceptions helps you signal when something goes wrong, so you or other parts of the program can handle the problem properly instead of continuing with wrong data.
Click to reveal answer
Which keyword is used to raise an exception in Python?
Araise
Bthrow
Cerror
Dexcept
✗ Incorrect
The correct keyword to raise an exception in Python is raise.
What will happen if you run this code?
raise TypeError("Wrong type")
AThe program ignores the error
BThe program prints 'Wrong type' and continues
CNothing happens
DThe program stops and shows a TypeError with message 'Wrong type'
✗ Incorrect
Raising an exception stops the program and shows the error message unless it is caught.
How do you raise a custom exception named MyError?
Araise Exception('MyError')
Bthrow MyError()
Craise MyError()
Derror MyError()
✗ Incorrect
You raise a custom exception by calling raise MyError().
Which of these is a correct way to raise a ValueError with message 'Invalid input'?
Athrow ValueError('Invalid input')
Braise ValueError('Invalid input')
Craise 'Invalid input'
Derror ValueError('Invalid input')
✗ Incorrect
The correct syntax is raise ValueError('Invalid input').
If an exception is raised but not caught, what happens?
AProgram stops and shows an error message
BProgram restarts automatically
CException is ignored
DProgram continues normally
✗ Incorrect
Uncaught exceptions stop the program and display an error.
Explain in your own words what it means to raise an exception and why you might want to do it.
Think about telling your program something went wrong.
You got /4 concepts.
Write a short Python example that raises a ValueError with a message when a number is negative.
Use an if statement to check the number, then raise the error.
You got /4 concepts.
Practice
(1/5)
1. What does the raise statement do in Python?
easy
A. It creates a new variable.
B. It prints a message to the screen.
C. It stops the program and signals an error.
D. It repeats a block of code.
Solution
Step 1: Understand the purpose of raise
The raise statement is used to stop the program when an error or unexpected situation occurs.
Step 2: Compare options with raise behavior
Only It stops the program and signals an error. correctly describes that raise stops the program and signals an error.
Final Answer:
It stops the program and signals an error. -> Option C
Quick Check:
raise = stop program on error [OK]
Hint: Remember: raise means stop and show error [OK]
Common Mistakes:
Thinking raise prints messages
Confusing raise with variable creation
Assuming raise repeats code
2. Which of the following is the correct syntax to raise a ValueError with a message "Invalid input"?
easy
A. raise ValueError("Invalid input")
B. throw ValueError("Invalid input")
C. raise new ValueError("Invalid input")
D. error ValueError("Invalid input")
Solution
Step 1: Recall Python syntax for raising exceptions
In Python, the correct way to raise an exception is using raise ExceptionType("message").
Step 2: Check each option
raise ValueError("Invalid input") uses correct syntax. Options B, C, and D use invalid keywords or extra words not used in Python.
Hint: Use 'raise ExceptionType("message")' exactly [OK]
Common Mistakes:
Using 'throw' instead of 'raise'
Adding 'new' keyword like other languages
Using 'error' keyword which doesn't exist
3. What will be the output of this code?
def check_age(age):
if age < 18:
raise ValueError("Too young")
return "Access granted"
try:
print(check_age(16))
except ValueError as e:
print(e)
medium
A. Too young
B. Access granted
C. ValueError exception not caught
D. No output
Solution
Step 1: Analyze function behavior with age 16
Since 16 < 18, the function raises a ValueError with message "Too young".
Step 2: Check exception handling in try-except
The exception is caught by the except block, which prints the error message "Too young".
Final Answer:
Too young -> Option A
Quick Check:
Exception message printed = Too young [OK]
Hint: Exception message prints if caught in except [OK]
Common Mistakes:
Assuming function returns 'Access granted'
Thinking exception crashes program
Missing that except prints the error message
4. Find the error in this code snippet:
def divide(a, b):
if b == 0:
raise "Cannot divide by zero"
return a / b
print(divide(10, 0))
medium
A. No error, code runs fine
B. raise must be followed by an exception instance, not a string
C. Division by zero is allowed in Python
D. Function divide should return None when b is zero
Solution
Step 1: Identify the raise statement usage
The code uses raise "Cannot divide by zero", which raises a string, not an exception object.
Step 2: Understand correct raise syntax
Python requires raising an exception instance, e.g., raise ValueError("Cannot divide by zero").
Final Answer:
raise must be followed by an exception instance, not a string -> Option B
Quick Check:
raise needs exception object, not string [OK]
Hint: Always raise an exception object, not a string [OK]
Common Mistakes:
Raising strings instead of exceptions
Ignoring that division by zero causes error
Assuming code runs without error
5. You want to create a function check_score(score) that raises a ValueError if the score is not between 0 and 100 (inclusive). Which code correctly implements this?
hard
A. def check_score(score):
if score <= 0 or score >= 100:
raise ValueError("Score must be 0-100")
return True
B. def check_score(score):
if 0 < score < 100:
raise ValueError("Score must be 0-100")
return True
C. def check_score(score):
if score == 0 or score == 100:
raise ValueError("Score must be 0-100")
return True
D. def check_score(score):
if score < 0 or score > 100:
raise ValueError("Score must be 0-100")
return True
Solution
Step 1: Understand the valid score range
Score must be between 0 and 100, including 0 and 100.
Step 2: Check each condition for raising ValueError
def check_score(score):
if score < 0 or score > 100:
raise ValueError("Score must be 0-100")
return True raises error if score is less than 0 or greater than 100, correctly allowing 0 and 100.
Step 3: Verify other options
def check_score(score):
if 0 < score < 100:
raise ValueError("Score must be 0-100")
return True raises error incorrectly for valid scores between 0 and 100. def check_score(score):
if score <= 0 or score >= 100:
raise ValueError("Score must be 0-100")
return True excludes 0 and 100 incorrectly. def check_score(score):
if score == 0 or score == 100:
raise ValueError("Score must be 0-100")
return True raises error only if score equals 0 or 100, which is wrong.
Final Answer:
def check_score(score):
if score < 0 or score > 100:
raise ValueError("Score must be 0-100")
return True -> Option D
Quick Check:
Raise error outside 0-100 inclusive = def check_score(score):
if score < 0 or score > 100:
raise ValueError("Score must be 0-100")
return True [OK]
Hint: Use 'if score < 0 or score > 100' to check range [OK]