What is the rescue modifier (inline form) in Ruby?
It is a way to handle exceptions in a single line using the rescue keyword after an expression. It returns the expression's value if no error occurs, or the rescue value if an error happens.
Click to reveal answer
beginner
How does the rescue modifier differ from a full begin-rescue-end block?
The rescue modifier is shorter and used inline after an expression, while begin-rescue-end is a block that can handle multiple lines and more complex error handling.
Click to reveal answer
beginner
Example: What does result = 10 / 0 rescue 'error' return?
It returns the string 'error' because dividing by zero raises an exception, which is caught by the rescue modifier.
Click to reveal answer
intermediate
Can the rescue modifier catch specific exception types?
No, the rescue modifier catches all StandardError exceptions by default. For specific exceptions, use a full begin-rescue block.
Click to reveal answer
beginner
Why use the rescue modifier instead of a full rescue block?
It makes code shorter and cleaner for simple error handling when you want to provide a fallback value quickly.
Click to reveal answer
What does the rescue modifier do in Ruby?
AHandles exceptions inline and returns a fallback value if an error occurs
BDefines a new method
CCreates a loop
DDeclares a variable
✗ Incorrect
The rescue modifier catches exceptions inline and returns the fallback value if an error happens.
Which of these is a correct use of the rescue modifier?
Arescue value = 5 / 0 'fail'
Bvalue = 5 / 0 rescue 'fail'
Cbegin value = 5 / 0 rescue 'fail' end
Dvalue = rescue 5 / 0 'fail'
✗ Incorrect
The rescue modifier is used after an expression, like value = 5 / 0 rescue 'fail'.
If no error occurs, what does the rescue modifier return?
AThe original expression's value
BThe rescue value
Cnil
DAn error message
✗ Incorrect
If no error happens, the rescue modifier returns the original expression's value.
Can the rescue modifier specify which exception to catch?
ANo, it catches all exceptions including system errors
BYes, by adding the exception name after rescue
CYes, by using a block
DNo, it catches all StandardError exceptions
✗ Incorrect
The rescue modifier catches all StandardError exceptions by default; specific exceptions require a full rescue block.
Which is a benefit of using the rescue modifier?
AHandles multiple exceptions separately
BAllows retrying the code automatically
CShorter code for simple error handling
DLogs errors to a file
✗ Incorrect
The rescue modifier makes code shorter and cleaner for simple fallback values.
Explain how the rescue modifier works in Ruby and when you might use it.
Think about how to handle errors quickly in one line.
You got /4 concepts.
Write a Ruby expression using the rescue modifier to divide two numbers and return 'error' if division fails.
Use the rescue keyword after the division.
You got /3 concepts.
Practice
(1/5)
1. What does the rescue modifier do in Ruby when used like value = 10 / x rescue 0?
easy
A. It ignores the division and returns nil
B. It always returns 10 divided by x
C. It returns 0 if dividing by x causes an error
D. It raises an error if x is zero
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
If 10 / x raises 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 C
Quick 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
A. result = some_method rescue fallback_value
B. result = rescue some_method fallback_value
C. result = some_method ? rescue fallback_value
D. result = some_method else rescue fallback_value
Solution
Step 1: Recall the rescue modifier syntax
The rescue modifier is placed after an expression, followed by the fallback value, like expression 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 A