Handling Errors with Multiple Rescue Clauses in Ruby
📖 Scenario: Imagine you are writing a small program that divides numbers. Sometimes, the user might enter zero or a wrong input, causing errors. You want to handle these errors nicely so the program doesn't crash.
🎯 Goal: You will build a Ruby program that divides two numbers and uses multiple rescue clauses to handle different types of errors separately.
📋 What You'll Learn
Create two variables with exact values for numerator and denominator
Create a variable called result initialized to nil
Use a begin block to perform division
Add multiple rescue clauses to handle ZeroDivisionError and TypeError
Print the result or an error message
💡 Why This Matters
🌍 Real World
Handling errors gracefully is important in real programs to avoid crashes and give helpful messages to users.
💼 Career
Knowing how to use multiple rescue clauses helps you write robust Ruby code that can handle different problems clearly.
Progress0 / 4 steps
1
Set up numerator and denominator variables
Create a variable called numerator and set it to 10. Create another variable called denominator and set it to 0.
Ruby
Hint
Use simple assignment like numerator = 10 and denominator = 0.
2
Initialize result variable
Create a variable called result and set it to nil.
Ruby
Hint
Use result = nil to start with no value.
3
Add begin block with multiple rescue clauses
Write a begin block where you set result to numerator / denominator. Add two rescue clauses: one for ZeroDivisionError that sets result to the string "Cannot divide by zero", and one for TypeError that sets result to "Invalid types for division".
Ruby
Hint
Use begin and rescue keywords to catch errors separately.
4
Print the result
Write puts result to display the value of result.
Ruby
Hint
Use puts result to show the output.
Practice
(1/5)
1. What is the purpose of using multiple rescue clauses in Ruby?
easy
A. To repeat the same error handling multiple times
B. To run code only when no errors occur
C. To handle different types of errors with specific responses
D. To skip error handling entirely
Solution
Step 1: Understand rescue clauses
Multiple rescue clauses allow you to catch different error types separately.
Step 2: Purpose of specific error handling
This helps you respond differently depending on the error type.
Final Answer:
To handle different types of errors with specific responses -> Option C
Quick Check:
Multiple rescue clauses = specific error handling [OK]
Hint: Multiple rescue clauses catch different errors separately [OK]
Common Mistakes:
Thinking rescue clauses run only when no errors occur
Assuming multiple rescue repeats same handling
Believing rescue skips error handling
2. Which of the following is the correct syntax for multiple rescue clauses in Ruby?
easy
A. try
# code
rescue ZeroDivisionError
# handle zero division
rescue StandardError
# handle other errors
end
B. begin
# code
catch ZeroDivisionError
# handle zero division
catch StandardError
# handle other errors
end
C. begin
# code
rescue ZeroDivisionError, StandardError
# handle errors
end
D. begin
# code
rescue ZeroDivisionError
# handle zero division
rescue StandardError
# handle other errors
end
Solution
Step 1: Identify Ruby error handling syntax
Ruby uses begin and rescue for error handling, not try or catch.
Step 2: Check multiple rescue clauses
Multiple rescue clauses are written one after another inside begin...end.
Final Answer:
begin
# code
rescue ZeroDivisionError
# handle zero division
rescue StandardError
# handle other errors
end -> Option D
Quick Check:
Ruby uses begin-rescue-end with multiple rescue [OK]
Hint: Use begin-rescue-end blocks for multiple error types [OK]
Common Mistakes:
Using try or catch instead of rescue
Combining errors in one rescue without comma
Incorrect block keywords
3. What will be the output of this Ruby code?
begin
1 / 0
rescue ZeroDivisionError
puts 'Zero division error caught'
rescue StandardError
puts 'Standard error caught'
end
medium
A. Zero division error caught
B. Standard error caught
C. No output, program crashes
D. SyntaxError
Solution
Step 1: Identify the error raised
The code 1 / 0 raises a ZeroDivisionError.
Step 2: Match error with rescue clauses
The first rescue clause catches ZeroDivisionError and prints the message.
Final Answer:
Zero division error caught -> Option A
Quick Check:
ZeroDivisionError triggers first rescue [OK]
Hint: Match error type to first rescue clause that fits [OK]
Common Mistakes:
Assuming StandardError rescue runs first
Thinking program crashes without rescue
Confusing syntax error with runtime error
4. Identify the error in this Ruby code with multiple rescue clauses:
begin
puts 'Start'
rescue
puts 'Rescue block'
rescue ZeroDivisionError
puts 'Zero division error'
end
medium
A. Missing ensure block causes error
B. Multiple rescue clauses after a generic rescue cause syntax error
C. No error, code runs fine
D. Rescue clauses must be inside methods only
Solution
Step 1: Check for syntax errors
In Ruby, a generic rescue clause must come after all specific rescue clauses. Placing a generic rescue before specific ones causes a syntax error.
Step 2: Analyze the code
The code places a generic rescue first, then a specific rescue, which is invalid syntax.
Final Answer:
Multiple rescue clauses after a generic rescue cause syntax error -> Option B
Quick Check:
Generic rescue must be last [OK]
Hint: Generic rescue clause must come after specific rescue clauses [OK]
Common Mistakes:
Thinking generic rescue before specific causes syntax error
Believing ensure block is mandatory
Assuming rescue clauses only work inside methods
5. Given this Ruby code, what will be printed?
def test
begin
raise ArgumentError, 'Wrong argument'
rescue ZeroDivisionError
puts 'Zero division error'
rescue ArgumentError
puts 'Argument error caught'
ensure
puts 'Always runs'
end
end
test
hard
A. Argument error caught
Always runs
B. Zero division error
Always runs
C. Always runs
Argument error caught
D. No output, error not handled
Solution
Step 1: Identify raised error
The code raises ArgumentError with message 'Wrong argument'.
Step 2: Match rescue clauses
The ArgumentError rescue clause runs and prints 'Argument error caught'.
Step 3: Understand ensure block
The ensure block always runs, printing 'Always runs'.
Final Answer:
Argument error caught
Always runs -> Option A
Quick Check:
ArgumentError rescued, ensure always runs [OK]
Hint: Ensure block runs after rescue regardless of error [OK]
Common Mistakes:
Thinking ensure runs before rescue
Assuming wrong rescue clause runs
Ignoring ensure block output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.