0
0
Rubyprogramming~10 mins

Multiple rescue clauses in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple rescue clauses
Start
Try block
Exception?
NoEnd
Yes
Rescue clause 1: matches?
NoRescue clause 2: matches?
Handle Exception
End
The program tries code, checks for exceptions, and handles them with the first matching rescue clause, then ends.
Execution Sample
Ruby
begin
  1 / 0
rescue ZeroDivisionError
  puts "Zero division error caught"
rescue StandardError
  puts "Standard error caught"
end
This code tries to divide by zero, catches the ZeroDivisionError first, and prints a message.
Execution Table
StepActionEvaluationResult
1Execute begin block: 1 / 0Raises ZeroDivisionErrorException raised
2Check rescue ZeroDivisionErrorMatches exceptionRescue clause 1 runs
3Execute rescue clause 1puts messageOutput: Zero division error caught
4End of begin-rescue blockNo more codeProgram continues
💡 Exception handled by first matching rescue clause, program continues normally
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
exceptionnilZeroDivisionError raisedHandled by rescue clause 1Handlednil
Key Moments - 2 Insights
Why does the first rescue clause run instead of the second?
Because the exception raised is ZeroDivisionError, which matches the first rescue clause exactly, so Ruby does not check further clauses (see execution_table step 2).
What happens if no rescue clause matches the exception?
The exception is unhandled and will cause the program to crash or propagate up (not shown in this example, but implied in concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what exception is raised at step 1?
ANo exception
BStandardError
CZeroDivisionError
DNameError
💡 Hint
Check the 'Evaluation' column in execution_table row 1
At which step does the program print the message 'Zero division error caught'?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Result' column in execution_table row 3
If the exception was a RuntimeError, which rescue clause would run?
ARescue clause 1 (ZeroDivisionError)
BRescue clause 2 (StandardError)
CNo rescue clause
DBoth rescue clauses
💡 Hint
StandardError is a parent of RuntimeError, so rescue clause 2 matches (concept_flow and key_moments)
Concept Snapshot
begin
  # code that might raise
rescue ExceptionType1
  # handle ExceptionType1
rescue ExceptionType2
  # handle ExceptionType2
end

- Ruby tries rescue clauses in order.
- First matching rescue handles the exception.
- If none match, exception propagates.
Full Transcript
This example shows how Ruby handles multiple rescue clauses. The program tries to run code that divides by zero, which raises a ZeroDivisionError. Ruby checks the first rescue clause and finds it matches the exception, so it runs that rescue block and prints a message. It does not check further rescue clauses. If no rescue clause matched, the exception would be unhandled and cause an error. This step-by-step trace helps understand how Ruby picks the right rescue clause.