What is the purpose of a begin/rescue/end block in Ruby?
It is used to handle errors (exceptions) that might happen during the execution of code inside the begin block, allowing the program to continue running instead of crashing.
Click to reveal answer
beginner
How do you catch a specific error type in a rescue block?
You specify the error class after <code>rescue</code>. For example: <code>rescue ZeroDivisionError</code> will catch only division by zero errors.
Click to reveal answer
beginner
What happens if an error occurs inside a begin block but there is no matching rescue block?
The program will stop and show the error message (raise the exception) because it was not handled.
Click to reveal answer
intermediate
Can you have multiple rescue clauses in one begin block? Why?
Yes, you can have multiple rescue clauses to handle different types of errors separately, making your error handling more precise.
Click to reveal answer
intermediate
What is the role of the ensure block in Ruby's error handling?
The ensure block runs code no matter what happens in the begin block, whether an error occurs or not. It's useful for cleanup actions like closing files.
Click to reveal answer
What keyword starts a block that handles exceptions in Ruby?
Atry
Bhandle
Ccatch
Dbegin
✗ Incorrect
In Ruby, begin starts a block where exceptions can be handled with rescue.
Which keyword is used to catch exceptions in Ruby?
Aexcept
Bcatch
Crescue
Dhandle
✗ Incorrect
rescue is used in Ruby to catch and handle exceptions.
What will happen if an exception is raised but no rescue block matches it?
AThe program crashes and shows the error
BThe program retries the code
CThe program ignores the error
DThe program logs the error silently
✗ Incorrect
If no matching rescue block is found, Ruby raises the exception and the program stops.
Which block runs code regardless of whether an exception occurs or not?
Arescue
Bensure
Cfinally
Dbegin
✗ Incorrect
ensure runs code no matter what, useful for cleanup.
How do you rescue multiple specific exceptions in one rescue clause?
Arescue Exception1, Exception2
Brescue Exception1 | Exception2
Crescue (Exception1, Exception2)
Drescue Exception1 & Exception2
✗ Incorrect
You list exceptions separated by commas after rescue to catch multiple types.
Explain how a begin/rescue/end block works in Ruby and why it is useful.
Think about how you catch mistakes in real life to keep things running smoothly.
You got /5 concepts.
Describe the difference between rescue and ensure blocks in Ruby error handling.
One catches problems, the other always runs no matter what.
You got /4 concepts.
Practice
(1/5)
1. What is the purpose of the begin/rescue/end block in Ruby?
easy
A. To declare variables
B. To create a loop that repeats code
C. To define a new method
D. To handle errors and prevent the program from crashing
Solution
Step 1: Understand the role of begin block
The begin block contains code that might cause an error.
Step 2: Understand the role of rescue block
The rescue block catches errors and handles them so the program doesn't crash.
Final Answer:
To handle errors and prevent the program from crashing -> Option D
Quick Check:
begin/rescue = error handling [OK]
Hint: Remember: begin/rescue is for catching errors safely [OK]
Common Mistakes:
Thinking begin/rescue creates loops
Confusing rescue with method definition
Using begin/rescue to declare variables
2. Which of the following is the correct syntax to handle exceptions using begin/rescue/end in Ruby?
easy
A. begin
risky_code
rescue
handle_error
end
B. try {
risky_code
} catch {
handle_error
}
C. if error then
handle_error
end
D. begin:
risky_code
except:
handle_error
Solution
Step 1: Identify Ruby syntax for error handling
Ruby uses begin, rescue, and end keywords for exceptions.
Step 2: Compare options with Ruby syntax
begin
risky_code
rescue
handle_error
end matches Ruby syntax exactly; others use syntax from other languages or incorrect keywords.
Final Answer:
begin\n risky_code\nrescue\n handle_error\nend -> Option A
Quick Check:
Ruby error handling = begin/rescue/end [OK]
Hint: Look for Ruby keywords: begin, rescue, end [OK]
Common Mistakes:
Using try/catch like other languages
Using if/else for error handling
Mixing Python or JavaScript syntax
3. What will be the output of this Ruby code?
begin
puts 10 / 0
rescue ZeroDivisionError
puts "Cannot divide by zero"
end
medium
A. Cannot divide by zero
B. 0
C. 10
D. ZeroDivisionError
Solution
Step 1: Identify the error in the code
The code tries to divide 10 by 0, which raises a ZeroDivisionError.
Step 2: Understand the rescue block
The rescue block catches ZeroDivisionError and prints "Cannot divide by zero" instead of crashing.
Final Answer:
Cannot divide by zero -> Option A
Quick Check:
ZeroDivisionError caught = message printed [OK]
Hint: Divide by zero triggers rescue message [OK]
Common Mistakes:
Expecting program to crash
Thinking it prints 0 or 10
Confusing error name with output
4. Find the error in this Ruby code snippet:
begin
puts 5 + "3"
rescue
puts "Error caught"
end
medium
A. The begin block should be replaced with try
B. The rescue block is missing the error type
C. You cannot add a number and a string directly
D. The code will run without errors
Solution
Step 1: Analyze the operation inside begin block
Adding 5 (number) and "3" (string) causes a TypeError in Ruby.
Step 2: Check error handling
The rescue block catches any error and prints "Error caught"; the error is due to incompatible types.
Final Answer:
You cannot add a number and a string directly -> Option C
Quick Check:
Type mismatch causes error caught [OK]
Hint: Check data types before adding values [OK]
Common Mistakes:
Thinking rescue needs error type always
Assuming code runs without error
Confusing begin with try
5. How can you modify this code to handle multiple specific errors separately?
begin
# risky code
rescue
puts "An error occurred"
end
hard
A. Use if statements inside begin block
B. Use multiple rescue clauses with specific error classes
C. Replace rescue with else to catch errors
D. Put all code inside rescue block
Solution
Step 1: Understand handling multiple errors
Ruby allows multiple rescue clauses to catch different error types separately.
Step 2: Apply multiple rescue clauses
Write rescue clauses like rescue ZeroDivisionError and rescue IOError to handle errors distinctly.
Final Answer:
Use multiple rescue clauses with specific error classes -> Option B
Quick Check:
Multiple rescue = specific error handling [OK]
Hint: Use separate rescue for each error type [OK]
Common Mistakes:
Using else instead of rescue
Trying to catch errors with if statements
Putting code inside rescue block
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.