0
0
Rubyprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Raise for throwing errors
Start
Code runs
Error condition?
NoContinue 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.