0
0
Rubyprogramming~10 mins

Why error handling uses rescue in Ruby - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why error handling uses rescue
Start program
Execute code block
Error occurs?
NoContinue normal flow
Yes
rescue catches error
Handle error gracefully
Continue or exit program
The program tries to run code. If an error happens, rescue catches it to handle it safely, so the program doesn't crash.
Execution Sample
Ruby
begin
  puts 10 / 0
rescue ZeroDivisionError
  puts "Can't divide by zero!"
end
This code tries to divide 10 by 0, which causes an error. rescue catches the error and prints a message instead of crashing.
Execution Table
StepActionEvaluationResult
1Execute puts 10 / 010 / 0Error: ZeroDivisionError raised
2rescue ZeroDivisionError catches errorError matches ZeroDivisionErrorControl moves to rescue block
3Execute puts "Can't divide by zero!"Print messageOutput: Can't divide by zero!
4End of begin-rescue blockNo more codeProgram continues safely
💡 Error is caught by rescue, so program does not crash and continues.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
ErrorNoneZeroDivisionError raisedCaught by rescueHandled
Key Moments - 3 Insights
Why doesn't the program crash when dividing by zero?
Because the rescue block catches the ZeroDivisionError at step 2 in the execution table, preventing a crash.
What happens if the error is not the one specified in rescue?
The error would not be caught and the program would crash, since rescue only handles specified errors (see step 2).
Does the code after rescue run if no error occurs?
No error means rescue block is skipped and program continues normal flow (not shown in this example).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 2?
AThe division is performed successfully
BThe program crashes
CThe error is caught by rescue
DThe program skips the rescue block
💡 Hint
Check the 'Action' and 'Result' columns at step 2 in the execution table.
At which step does the program print the message "Can't divide by zero!"?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column for printing output in the execution table.
If the rescue block was missing, what would happen at step 2?
AThe error would be caught anyway
BThe program would crash due to unhandled error
CThe program would print the message anyway
DThe program would skip the error
💡 Hint
Refer to the explanation in key moments about what happens if error is not rescued.
Concept Snapshot
Use begin...rescue to handle errors in Ruby.
If an error happens in begin block, rescue catches it.
This prevents program crash and allows graceful handling.
Specify error type after rescue to catch specific errors.
Without rescue, errors stop the program immediately.
Full Transcript
This example shows how Ruby uses rescue to handle errors. The program tries to divide 10 by zero, which causes a ZeroDivisionError. Instead of crashing, the rescue block catches this error and prints a friendly message. This way, the program continues safely. Rescue only catches errors specified after it, so other errors will still crash the program if not handled. Using rescue helps keep programs running smoothly even when unexpected problems happen.