The rescue modifier lets you handle errors quickly in one line. It helps keep your code simple and clean when you expect something might fail.
Rescue modifier (inline form) in Ruby
Introduction
Syntax
Ruby
expression rescue fallback_valueThe code tries to run expression.
If an error happens, it returns fallback_value instead.
Examples
Ruby
result = 10 / 0 rescue 'Cannot divide by zero'
upcase on nil causes an error, so it prints the fallback string.Ruby
name = nil puts (name.upcase rescue 'No name given')
Ruby
number = Integer('abc') rescue 0
Sample Program
This program defines a method that divides two numbers. If dividing by zero happens, it returns an error message instead of crashing.
Ruby
def safe_divide(a, b) a / b rescue 'Error: division by zero' end puts safe_divide(10, 2) puts safe_divide(10, 0)
Important Notes
The rescue modifier only catches StandardError and its subclasses.
Use it for simple error handling, but for complex cases, use full begin...rescue blocks.
Summary
The rescue modifier helps handle errors in one line.
It tries the expression and returns a fallback if an error occurs.
Great for simple, quick error handling without extra code.
Practice
1. What does the rescue modifier do in Ruby when used like
value = 10 / x rescue 0?easy
Solution
Step 1: Understand the rescue modifier usage
The rescue modifier catches exceptions raised by the expression before it and returns the value after rescue if an error occurs.Step 2: Apply to the example
If10 / xraises an error (like division by zero), the rescue modifier returns 0 instead of crashing.Final Answer:
It returns 0 if dividing by x causes an error -> Option CQuick Check:
rescue modifier returns fallback on error [OK]
Hint: Rescue modifier returns fallback if error happens [OK]
Common Mistakes:
- Thinking rescue modifier prevents all errors
- Assuming it always returns the original expression
- Confusing rescue modifier with if-else
2. Which of the following is the correct syntax for using the rescue modifier inline in Ruby?
easy
Solution
Step 1: Recall the rescue modifier syntax
The rescue modifier is placed after an expression, followed by the fallback value, likeexpression rescue fallback.Step 2: Match with options
Only result = some_method rescue fallback_value matches this syntax correctly:result = some_method rescue fallback_value.Final Answer:
result = some_method rescue fallback_value -> Option AQuick Check:
Correct rescue modifier syntax = result = some_method rescue fallback_value [OK]
Hint: Rescue modifier goes after expression, then fallback [OK]
Common Mistakes:
- Placing rescue before the expression
- Using question marks or else with rescue modifier
- Confusing rescue modifier with rescue block
3. What is the output of this Ruby code?
value = Integer('abc') rescue 'error'
puts valuemedium
Solution
Step 1: Understand Integer conversion with invalid string
CallingInteger('abc')raises an ArgumentError because 'abc' is not a valid number.Step 2: Apply rescue modifier
The rescue modifier catches this error and returns the string 'error' instead of crashing.Final Answer:
error -> Option BQuick Check:
Invalid Integer conversion rescued to 'error' [OK]
Hint: Rescue returns fallback when conversion fails [OK]
Common Mistakes:
- Expecting the original string as output
- Thinking rescue returns nil by default
- Assuming code raises exception without rescue
4. Identify the error in this Ruby code snippet using rescue modifier:
result = 10 / 0 rescue puts 'Error!'
medium
Solution
Step 1: Analyze rescue modifier with puts
The rescue modifier catches the division by zero error and executesputs 'Error!', which prints the message.Step 2: Understand return value of puts
putsreturns nil, soresultis assigned nil after rescue.Final Answer:
No error; prints 'Error!' and result is nil -> Option AQuick Check:
Rescue modifier runs puts and assigns nil [OK]
Hint: Rescue modifier returns last expression value after rescue [OK]
Common Mistakes:
- Expecting rescue modifier to return printed string
- Thinking rescue modifier causes syntax error here
- Assuming result keeps original expression value
5. You want to safely convert user input to integer but return -1 if conversion fails. Which code correctly uses rescue modifier for this?
hard
Solution
Step 1: Understand rescue modifier placement
The rescue modifier must come after the expression to catch exceptions and provide fallback.Step 2: Check options for correct syntax and logic
Only num = Integer(user_input) rescue -1 correctly places rescue afterInteger(user_input)and returns -1 on failure.Final Answer:
num = Integer(user_input) rescue -1 -> Option DQuick Check:
Correct rescue modifier usage for fallback = num = Integer(user_input) rescue -1 [OK]
Hint: Put rescue after expression, then fallback value [OK]
Common Mistakes:
- Placing rescue before expression
- Using invalid syntax with question marks or else
- Forgetting rescue modifier for error handling
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
