Bird
Raised Fist0
Rubyprogramming~10 mins

Raise for throwing errors in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Raise for throwing errors
Start
↓
Code runs
↓
Error condition?
No→Continue normal flow
Yes↓
Raise error
↓
Error thrown
↓
Catch or terminate
The program runs code, checks for an error condition, raises an error if needed, then either catches it or stops.
Execution Sample
Ruby
def check_age(age)
  raise "Age must be positive" if age <= 0
  "Age is #{age}"
end

puts check_age(5)
puts check_age(-1)
This code raises an error if age is not positive, otherwise returns a message.
Execution Table
StepActionConditionResultOutput/Error
1Call check_age(5)age <= 0? (5 <= 0)False"Age is 5"
2Return from check_age"Age is 5" printed
3Call check_age(-1)age <= 0? (-1 <= 0)TrueRaise error with message "Age must be positive"
4Error thrownProgram stops with RuntimeError: Age must be positive
💡 Error raised at step 3 because age is -1, which is not positive
Variable Tracker
VariableStartAfter call 1After call 2Final
ageundefined5-1-1
Key Moments - 2 Insights
Why does the program stop after the second call to check_age?
Because at step 3 in the execution table, the condition age <= 0 is true, so raise throws an error that stops the program.
What happens if the condition for raise is false?
As shown in step 1, if the condition is false, the program continues normally and returns the string without error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after the first call to check_age?
Anil
BRuntimeError
C"Age is 5"
D"Age must be positive"
💡 Hint
Check step 2 in the execution table where the output is printed.
At which step does the program raise an error?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Result' column in the execution table for when raise is called.
If we change age to 10 in the second call, what happens?
AProgram prints "Age is 10" and continues
BError is raised
CProgram stops immediately
DNothing is printed
💡 Hint
Refer to the variable_tracker and execution_table for when age is positive.
Concept Snapshot
Use raise to throw an error in Ruby.
Syntax: raise "error message".
If condition is true, raise stops execution and shows error.
If false, code continues normally.
Useful to catch invalid inputs or unexpected states.
Full Transcript
This example shows how Ruby's raise keyword works to throw errors. The program calls a function check_age with a number. If the number is less than or equal to zero, raise triggers an error with a message. Otherwise, it returns a string showing the age. The execution table traces each step: first call with 5 passes and prints a message. Second call with -1 triggers raise, stopping the program with an error. The variable tracker shows how the age variable changes with each call. Key moments clarify why the program stops and what happens when the condition is false. The quiz tests understanding of when errors occur and outputs appear. The snapshot summarizes how to use raise simply and effectively.

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

  1. Step 1: Understand the purpose of raise

    The raise keyword is used to stop the program and signal that an error has occurred.
  2. Step 2: Compare with other options

    Printing messages, creating variables, or repeating code are not what raise does.
  3. Final Answer:

    It stops the program and throws an error. -> Option A
  4. 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

  1. Step 1: Recall Ruby syntax for raising errors

    In Ruby, raise followed by a string message raises a RuntimeError with that message.
  2. 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.
  3. Final Answer:

    raise "Error happened" -> Option A
  4. 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

  1. 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.
  2. Step 2: Determine the returned value and output

    Since no error is raised, the method returns "Access granted" which is printed by puts.
  3. Final Answer:

    Access granted -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Using assignment (=) instead of comparison (==) in the if condition -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    raise ArgumentError, "Invalid input" if number < 0 -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard