Bird
Raised Fist0
Rubyprogramming~20 mins

Multiple rescue clauses in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Rescue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of multiple rescue clauses with different exceptions
What is the output of this Ruby code with multiple rescue clauses?
Ruby
begin
  1 / 0
rescue ZeroDivisionError
  puts "ZeroDivisionError caught"
rescue StandardError
  puts "StandardError caught"
end
AStandardError caught
BZeroDivisionError caught
CNo output, program crashes
DRuntimeError caught
Attempts:
2 left
💡 Hint
Think about which error is raised by dividing by zero in Ruby.
❓ Predict Output
intermediate
2:00remaining
Which rescue clause handles the exception?
What will this Ruby code print?
Ruby
begin
  [].fetch(10)
rescue IndexError
  puts "IndexError handled"
rescue KeyError
  puts "KeyError handled"
end
AKeyError handled
BArgumentError handled
CNo output, program crashes
DIndexError handled
Attempts:
2 left
💡 Hint
Check what exception is raised when fetching an invalid index from an array.
❓ Predict Output
advanced
2:00remaining
Output when multiple exceptions are rescued with one clause
What does this Ruby code output?
Ruby
begin
  {}.fetch(:foo)
rescue KeyError, NoMethodError
  puts "KeyError or NoMethodError rescued"
rescue StandardError
  puts "StandardError rescued"
end
AKeyError or NoMethodError rescued
BStandardError rescued
CNo output, program crashes
DTypeError rescued
Attempts:
2 left
💡 Hint
Accessing a missing key in a hash with [] returns nil, but with fetch it raises KeyError.
❓ Predict Output
advanced
2:00remaining
Which rescue clause runs with nested exceptions?
What will this Ruby code print?
Ruby
begin
  begin
    raise ArgumentError, "wrong argument"
  rescue TypeError
    puts "TypeError rescued"
  end
rescue ArgumentError
  puts "ArgumentError rescued"
end
ANo output, program crashes
BTypeError rescued
CArgumentError rescued
DRuntimeError rescued
Attempts:
2 left
💡 Hint
The inner rescue only catches TypeError, but the raised error is ArgumentError.
❓ Predict Output
expert
2:00remaining
Output of multiple rescue clauses with else and ensure
What is the output of this Ruby code?
Ruby
begin
  puts "Start"
  10 / 2
rescue ZeroDivisionError
  puts "ZeroDivisionError caught"
rescue StandardError
  puts "StandardError caught"
else
  puts "No errors"
ensure
  puts "Always runs"
end
A
Start
No errors
Always runs
B
Start
ZeroDivisionError caught
Always runs
C
Start
Always runs
D
Start
StandardError caught
Always runs
Attempts:
2 left
💡 Hint
The else clause runs only if no exception is raised.

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

  1. Step 1: Understand rescue clauses

    Multiple rescue clauses allow you to catch different error types separately.
  2. Step 2: Purpose of specific error handling

    This helps you respond differently depending on the error type.
  3. Final Answer:

    To handle different types of errors with specific responses -> Option C
  4. 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

  1. Step 1: Identify Ruby error handling syntax

    Ruby uses begin and rescue for error handling, not try or catch.
  2. Step 2: Check multiple rescue clauses

    Multiple rescue clauses are written one after another inside begin...end.
  3. Final Answer:

    begin # code rescue ZeroDivisionError # handle zero division rescue StandardError # handle other errors end -> Option D
  4. 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

  1. Step 1: Identify the error raised

    The code 1 / 0 raises a ZeroDivisionError.
  2. Step 2: Match error with rescue clauses

    The first rescue clause catches ZeroDivisionError and prints the message.
  3. Final Answer:

    Zero division error caught -> Option A
  4. 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

  1. 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.
  2. Step 2: Analyze the code

    The code places a generic rescue first, then a specific rescue, which is invalid syntax.
  3. Final Answer:

    Multiple rescue clauses after a generic rescue cause syntax error -> Option B
  4. 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

  1. Step 1: Identify raised error

    The code raises ArgumentError with message 'Wrong argument'.
  2. Step 2: Match rescue clauses

    The ArgumentError rescue clause runs and prints 'Argument error caught'.
  3. Step 3: Understand ensure block

    The ensure block always runs, printing 'Always runs'.
  4. Final Answer:

    Argument error caught Always runs -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard