Multiple rescue Clauses
Part of the Logic & Flow section of Coddy's Ruby journey — lesson 45 of 56.
One begin block can have several rescue clauses. Ruby checks them top to bottom and runs the first one whose exception class matches.
begin
number = Integer(gets.chomp)
result = 100 / number
puts result
rescue ArgumentError
puts "That's not a valid number."
rescue ZeroDivisionError
puts "Can't divide by zero."
endOrder matters: list specific classes before general ones. StandardError placed first would swallow everything and the later branches would never run.
You can also catch multiple exception classes in one branch by listing them with commas:
begin
risky_call
rescue ArgumentError, TypeError => e
puts "Bad input: #{e.message}"
endUse this when the recovery code is the same for several failure modes.
Challenge
EasyRead two lines from input, both meant to be integers. Use Integer(...) for both, then divide the first by the second.
Handle three cases with separate rescue branches:
ArgumentError→ printBad number!ZeroDivisionError→ printCannot divide by zero!TypeError→ printWrong type!
If everything works, print Result: <a / b> (integer division).
For input 10 / 2, the output is Result: 5. For 10 / 0, Cannot divide by zero!. For 10 / abc, Bad number!.
Cheat sheet
A begin block can have multiple rescue clauses — Ruby matches them top to bottom:
begin
number = Integer(gets.chomp)
result = 100 / number
puts result
rescue ArgumentError
puts "That's not a valid number."
rescue ZeroDivisionError
puts "Can't divide by zero."
endList specific exception classes before general ones — a general class like StandardError placed first would swallow all subsequent branches.
Catch multiple exception classes in one branch by separating them with commas:
rescue ArgumentError, TypeError => e
puts "Bad input: #{e.message}"Try it yourself
raw_a = gets.chomp
raw_b = gets.chomp
# TODO: begin ... rescue ArgumentError ... rescue ZeroDivisionError ... rescue TypeError ... end
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Strings In Depth
String Methods OverviewString InterpolationIterating Over StringsSplit and JoinRecap - String Weaver4Blocks, Procs & Lambdas
What is a Block?do..end vs BracesThe yield KeywordBlock ParametersProcs and LambdasRecap - Custom Iterator7Hashes Part 2
Hash.new with DefaultsIterating HashesNested HashesMerging and TransformingRecap - Frequency Counter10Project - Student Records
Project OverviewAdd Student5Enumerable Powerhouse
Select and RejectChaining MapReduce / Injectcount, all?, any?, none?group_by and partitionsort_by, min_by, max_byRecap - Data Pipeline8Advanced Decision Making
Case with Classes & RegexMulti-value whenTernary OperatorInline if / unlessRecap - Grade Classifier32D Arrays
2D Array BasicsAccessing 2D ElementsIterating Over 2D ArraysCommon 2D PatternsRecap - Matrix Operations9Error Handling
What is an Exception?begin / rescueMultiple rescue Clausesensure and raiseRecap - Safe Calculator