Bird
Raised Fist0
Rubyprogramming~20 mins

Why error handling uses rescue in Ruby - Challenge Your Understanding

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
What is the output of this Ruby code using rescue?
Look at this Ruby code. What will it print when run?
Ruby
def divide(a, b)
  a / b
rescue ZeroDivisionError
  "Cannot divide by zero"
end

puts divide(10, 0)
ACannot divide by zero
B0
C10
DZeroDivisionError
Attempts:
2 left
💡 Hint
Think about what happens when you divide by zero in Ruby and how rescue handles it.
🧠 Conceptual
intermediate
1:30remaining
Why do we use rescue in Ruby error handling?
Choose the best reason why Ruby uses rescue for error handling.
ATo automatically fix all errors without programmer input
BTo catch and handle errors so the program can continue running
CTo make the code shorter without any error handling
DTo speed up the program execution
Attempts:
2 left
💡 Hint
Think about what happens if an error is not handled in a program.
🔧 Debug
advanced
2:00remaining
What error does this Ruby code raise?
This code tries to open a file and read it. What error will it raise?
Ruby
def read_file(filename)
  File.open(filename) do |file|
    file.read
  end
rescue
  "File not found"
end

puts read_file("missing.txt")
AErrno::ENOENT
BSyntaxError
CFileNotFoundError
DNoMethodError
Attempts:
2 left
💡 Hint
Check Ruby's error class for missing files.
❓ Predict Output
advanced
2:30remaining
What is the output of this nested rescue example?
What will this Ruby code print?
Ruby
def test
  begin
    raise "Oops"
  rescue RuntimeError
    begin
      raise "Inner error"
    rescue
      "Handled inner"
    end
  end
end

puts test
ARuntimeError
BOops
CHandled inner
DUnhandled exception
Attempts:
2 left
💡 Hint
Look at how the inner rescue handles the second raise.
🚀 Application
expert
3:00remaining
How many times will the rescue block run in this code?
Count how many times the rescue block executes in this Ruby code.
Ruby
count = 0
3.times do |i|
  begin
    raise "Error at #{i}" if i.even?
  rescue
    count += 1
  end
end

puts count
A1
B0
C3
D2
Attempts:
2 left
💡 Hint
Check which values of i are even in the loop.

Practice

(1/5)
1. Why do Ruby programs use rescue in error handling?
easy
A. To make the code shorter
B. To speed up the program execution
C. To catch errors and prevent the program from crashing
D. To automatically fix all errors

Solution

  1. Step 1: Understand the purpose of rescue

    rescue is used to catch errors that happen during program execution.
  2. Step 2: Recognize the benefit of catching errors

    Catching errors prevents the program from crashing and allows it to handle problems gracefully.
  3. Final Answer:

    To catch errors and prevent the program from crashing -> Option C
  4. Quick Check:

    rescue catches errors = C [OK]
Hint: Remember: rescue stops crashes by catching errors [OK]
Common Mistakes:
  • Thinking rescue speeds up code
  • Believing rescue fixes errors automatically
  • Confusing rescue with code shortening
2. Which of the following is the correct syntax to handle errors using rescue in Ruby?
easy
A. error do # handle error end
B. try # code catch # error handling end
C. if error # handle error end
D. begin # code rescue # error handling end

Solution

  1. Step 1: Identify Ruby's error handling syntax

    Ruby uses begin and rescue blocks to handle errors.
  2. Step 2: Compare options to Ruby syntax

    begin # code rescue # error handling end matches Ruby's syntax exactly; others use syntax from other languages or incorrect forms.
  3. Final Answer:

    begin\n # code\nrescue\n # error handling\nend -> Option D
  4. Quick Check:

    Ruby uses begin-rescue-end = D [OK]
Hint: Remember Ruby uses begin and rescue keywords [OK]
Common Mistakes:
  • Using try-catch like other languages
  • Using if statements for error handling
  • Misplacing rescue outside begin-end
3. What will be the output of this Ruby code?
begin
  puts 10 / 0
rescue ZeroDivisionError
  puts "Cannot divide by zero!"
end
medium
A. 0
B. Cannot divide by zero!
C. Infinity
D. Error: division by zero

Solution

  1. Step 1: Identify the error raised

    Dividing 10 by 0 raises a ZeroDivisionError in Ruby.
  2. Step 2: Check the rescue block handling

    The rescue block catches ZeroDivisionError and prints "Cannot divide by zero!".
  3. Final Answer:

    Cannot divide by zero! -> Option B
  4. Quick Check:

    ZeroDivisionError caught prints message = A [OK]
Hint: Look for error type matched in rescue block [OK]
Common Mistakes:
  • Expecting program to crash with error message
  • Thinking division by zero returns 0 or Infinity
  • Ignoring rescue block effect
4. Find the error in this Ruby code snippet:
begin
  puts 5 + '3'
rescue
  puts "Error caught!"
end
medium
A. Adding integer and string causes a TypeError
B. The rescue block is missing the error type
C. The begin keyword is not needed
D. The puts statement is incorrect

Solution

  1. Step 1: Analyze the operation inside begin block

    Adding 5 (integer) and '3' (string) causes a TypeError in Ruby.
  2. Step 2: Understand rescue block behavior

    The rescue block catches the error and prints "Error caught!"; the error is due to incompatible types.
  3. Final Answer:

    Adding integer and string causes a TypeError -> Option A
  4. Quick Check:

    TypeError from adding int and string = A [OK]
Hint: Check data types when rescue catches errors [OK]
Common Mistakes:
  • Thinking rescue must specify error type
  • Believing begin is optional here
  • Assuming puts causes error
5. You want to handle different errors separately in Ruby. Which code correctly uses rescue to handle ZeroDivisionError and NoMethodError differently?
begin
  # some code
rescue ZeroDivisionError
  puts "Divide by zero error"
rescue NoMethodError
  puts "Method not found error"
end
hard
A. This code correctly handles each error with separate rescue blocks
B. You cannot have multiple rescue blocks; only one is allowed
C. You must use if statements inside rescue to check error type
D. Rescue only catches StandardError, so this won't work

Solution

  1. Step 1: Understand multiple rescue usage

    Ruby allows multiple rescue clauses to handle different error types separately.
  2. Step 2: Verify code matches correct syntax

    The code uses separate rescue blocks for ZeroDivisionError and NoMethodError, which is valid.
  3. Final Answer:

    This code correctly handles each error with separate rescue blocks -> Option A
  4. Quick Check:

    Multiple rescue blocks handle errors separately = B [OK]
Hint: Use multiple rescue blocks for different errors [OK]
Common Mistakes:
  • Thinking only one rescue block is allowed
  • Trying to use if inside rescue instead
  • Believing rescue can't catch specific errors
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