📖 Scenario: You are building a simple program that checks if a user is old enough to access a website. If the user is too young, the program should stop and show an error message.
🎯 Goal: Learn how to use raise in Ruby to throw errors when something goes wrong.
📋 What You'll Learn
Create a variable with the user's age
Create a minimum age variable for access
Use raise to throw an error if the user is too young
Print a success message if the user is old enough
💡 Why This Matters
🌍 Real World
Throwing errors is important in real programs to stop when something is wrong, like invalid user input or missing files.
💼 Career
Understanding how to raise errors helps you write safer code and handle problems clearly, a key skill for any Ruby developer.
Progress0 / 4 steps
1
Set up the user's age
Create a variable called user_age and set it to 15.
Ruby
Hint
Use = to assign the value 15 to user_age.
2
Set the minimum age for access
Create a variable called minimum_age and set it to 18.
Ruby
Hint
Use = to assign the value 18 to minimum_age.
3
Use raise to throw an error if too young
Write an if statement that checks if user_age is less than minimum_age. Inside the if, use raise with the message "Access denied: You are too young!".
Ruby
Hint
Use raise inside the if block to stop the program with an error message.
4
Print success message if age is okay
Add an else block after the if that prints "Access granted: Welcome!".
Ruby
Hint
Use puts to print the welcome message inside the else block.
Practice
(1/5)
1. What does the raise keyword do in Ruby?
easy
A. It stops the program and throws an error.
B. It prints a message to the screen.
C. It creates a new variable.
D. It repeats a block of code.
Solution
Step 1: Understand the purpose of raise
The raise keyword is used to stop the program and signal that an error has occurred.
Step 2: Compare with other options
Printing messages, creating variables, or repeating code are not what raise does.
Final Answer:
It stops the program and throws an error. -> Option A
Quick Check:
raise stops program = D [OK]
Hint: Remember: raise means stop and show error [OK]
Common Mistakes:
Thinking raise prints messages
Confusing raise with loops
Assuming raise creates variables
2. Which of the following is the correct syntax to raise a runtime error with a message in Ruby?
easy
A. raise "Error happened"
B. throw "Error happened"
C. error.raise("Error happened")
D. raise: "Error happened"
Solution
Step 1: Recall Ruby syntax for raising errors
In Ruby, raise followed by a string message raises a RuntimeError with that message.
Step 2: Check each option
raise "Error happened" uses correct syntax. throw "Error happened" uses throw which is different. error.raise("Error happened") is invalid method call. raise: "Error happened" has wrong colon usage.
Final Answer:
raise "Error happened" -> Option A
Quick Check:
Correct raise syntax = A [OK]
Hint: Use raise followed by message in quotes [OK]
Common Mistakes:
Using throw instead of raise
Adding colon after raise
Calling raise as a method on error
3. What will be the output of this Ruby code?
def check_age(age)
raise "Age must be 18 or older" if age < 18
"Access granted"
end
puts check_age(20)
medium
A. Age must be 18 or older
B. No output
C. RuntimeError: Age must be 18 or older
D. Access granted
Solution
Step 1: Analyze the condition in the method
The method raises an error only if age is less than 18. Here, age is 20, so no error is raised.
Step 2: Determine the returned value and output
Since no error is raised, the method returns "Access granted" which is printed by puts.
Final Answer:
Access granted -> Option D
Quick Check:
Age 20 >= 18, no error = B [OK]
Hint: Raise only triggers if condition is true [OK]
Common Mistakes:
Assuming error always raises
Confusing error message with output
Ignoring the if condition
4. Identify the error in this Ruby code snippet:
def divide(a, b)
raise "Cannot divide by zero" if b = 0
a / b
end
puts divide(10, 0)
medium
A. Division operator is incorrect
B. Missing parentheses in raise statement
C. Using assignment (=) instead of comparison (==) in the if condition
D. Method divide is not defined
Solution
Step 1: Check the if condition syntax
The condition uses b = 0 which assigns 0 to b instead of comparing it. This is a common mistake.
Step 2: Understand the impact
Because of assignment, b becomes 0, and since 0 is falsy in Ruby, the condition evaluates to false, so the raise is not triggered. The intended check is b == 0.
Final Answer:
Using assignment (=) instead of comparison (==) in the if condition -> Option C
Quick Check:
Use == for comparison, not = [OK]
Hint: Use == to compare, not = which assigns [OK]
Common Mistakes:
Confusing = and ==
Thinking raise needs parentheses
Assuming divide method is missing
5. You want to raise a custom error type ArgumentError with a message "Invalid input" when a method receives a negative number. Which code correctly does this?
hard
A. ArgumentError.raise("Invalid input") if number < 0
B. raise ArgumentError, "Invalid input" if number < 0
C. raise "ArgumentError: Invalid input" if number < 0
D. raise ArgumentError("Invalid input") if number < 0
Solution
Step 1: Understand how to raise custom error types
In Ruby, you can raise a specific error class by passing it as the first argument to raise, followed by the message.
Step 2: Check each option's syntax
raise ArgumentError, "Invalid input" if number < 0 correctly uses raise ArgumentError, "Invalid input". raise "ArgumentError: Invalid input" if number < 0 treats the error as a string only. ArgumentError.raise("Invalid input") if number < 0 is invalid as raise is not a method on ArgumentError. raise ArgumentError("Invalid input") if number < 0 is invalid syntax.
Final Answer:
raise ArgumentError, "Invalid input" if number < 0 -> Option B
Quick Check:
Raise with error class and message = C [OK]
Hint: Use raise ErrorClass, "message" for custom errors [OK]
Common Mistakes:
Passing error as string only
Using wrong syntax with parentheses
Confusing new method call with raise
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.