0
0
Rubyprogramming~10 mins

Rescue modifier (inline form) in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Rescue modifier (inline form)
Try expression
Success?
YesReturn value
No
Rescue exception
Return rescue value
The rescue modifier tries to run an expression and if an error happens, it returns a fallback value instead.
Execution Sample
Ruby
result = 10 / 0 rescue "Error"
puts result
This code tries to divide 10 by 0, which causes an error, so it returns "Error" instead.
Execution Table
StepExpressionError Raised?Rescue Triggered?Result
110 / 0Yes (ZeroDivisionError)Yes"Error" assigned to result
2puts resultNoNoPrints: Error
💡 Execution continues after printing the rescue value because the error was handled inline.
Variable Tracker
VariableStartAfter Step 1After Step 2
resultnil"Error""Error"
Key Moments - 3 Insights
Why does the code not crash when dividing by zero?
Because the rescue modifier catches the ZeroDivisionError inline and assigns "Error" to result, as shown in step 1 of the execution_table.
Does the rescue modifier catch all errors?
No, it only catches StandardError and its subclasses by default, which includes ZeroDivisionError here, as seen in the rescue triggered column.
What happens if no error occurs?
The expression result is assigned normally without rescue, so the rescue part is skipped, as would be shown if the expression succeeded.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after step 1?
A0
B"Error"
Cnil
D10
💡 Hint
Check the 'Result' column in step 1 where the rescue assigns "Error" to result.
At which step does the rescue modifier handle the error?
AStep 2
BNo rescue triggered
CStep 1
DBefore step 1
💡 Hint
Look at the 'Rescue Triggered?' column in the execution_table.
If the division was 10 / 2 instead, what would 'result' be after step 1?
A5
B"Error"
Cnil
D0
💡 Hint
If no error occurs, rescue is skipped and the expression result is assigned normally.
Concept Snapshot
rescue modifier syntax:
expression rescue fallback_value

Runs expression; if error occurs, returns fallback_value instead.
Only rescues StandardError and subclasses.
Useful for short error handling inline.
Example: x = 1/0 rescue "fail"
Full Transcript
The rescue modifier in Ruby lets you try an expression and if it causes an error, you get a fallback value instead. For example, dividing by zero causes an error, but with rescue modifier, you can catch it inline and assign a string like "Error". The flow is: try the expression, if no error, return its value; if error, return the rescue value. This is shown in the execution table where dividing 10 by 0 raises ZeroDivisionError, rescue triggers, and "Error" is assigned to result. Then printing result outputs "Error". This modifier only catches StandardError and subclasses. If no error happens, rescue is skipped and the expression result is used. This is a simple way to handle errors without writing full begin-rescue-end blocks.