0
0
Rubyprogramming~15 mins

Raise for throwing errors in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Raise for Throwing Errors in Ruby
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use puts to print the welcome message inside the else block.