Bird
Raised Fist0
Rubyprogramming~20 mins

Begin/rescue/end blocks in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple rescue block
What is the output of this Ruby code?
Ruby
def test
  begin
    1 / 0
  rescue ZeroDivisionError
    "divided by zero"
  end
end

puts test
AZeroDivisionError
Bdivided by zero
C0
DInfinity
Attempts:
2 left
💡 Hint
Look at what happens when dividing by zero inside the begin block and how rescue handles it.
❓ Predict Output
intermediate
2:00remaining
Return value with ensure block
What will this Ruby code print?
Ruby
def example
  begin
    return 1
  ensure
    puts "ensure block"
  end
end

puts example
A1
Bensure block
C
ensure block
1
D
1
ensure block
Attempts:
2 left
💡 Hint
Remember that ensure runs even if there is a return inside begin.
🔧 Debug
advanced
2:00remaining
Identify the error in rescue syntax
Which option contains a syntax error in the rescue block?
Ruby
begin
  puts 10 / 0
rescue ZeroDivisionError => e
  puts e.message
end
A
begin
  puts 10 / 0
rescue ZeroDivisionError => e
  puts e.message
end
B
begin
  puts 10 / 0
rescue ZeroDivisionError, NameError => e
  puts e.message
end
C
begin
  puts 10 / 0
rescue ZeroDivisionError
  puts 'error'
end
D
begin
  puts 10 / 0
rescue => e ZeroDivisionError
  puts e.message
end
Attempts:
2 left
💡 Hint
Check the placement of the exception class and variable in rescue syntax.
🧠 Conceptual
advanced
2:00remaining
Behavior of nested begin/rescue blocks
What will be the output of this Ruby code?
Ruby
begin
  begin
    raise "error"
  rescue RuntimeError
    puts "inner rescue"
    raise
  end
rescue
  puts "outer rescue"
end
A
inner rescue
outer rescue
B
outer rescue
inner rescue
Cinner rescue
Douter rescue
Attempts:
2 left
💡 Hint
Consider how the inner rescue handles the error and what happens when it re-raises it.
❓ Predict Output
expert
2:00remaining
Output with multiple rescue clauses and ensure
What is the output of this Ruby code?
Ruby
def test
  begin
    raise ArgumentError, "wrong argument"
  rescue ZeroDivisionError
    puts "zero division"
  rescue ArgumentError => e
    puts e.message
  ensure
    puts "always runs"
  end
end

test
A
wrong argument
always runs
B
zero division
always runs
C
always runs
wrong argument
D
ArgumentError
always runs
Attempts:
2 left
💡 Hint
Check which rescue clause matches the raised exception and the order of ensure execution.

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

  1. Step 1: Understand the role of begin block

    The begin block contains code that might cause an error.
  2. Step 2: Understand the role of rescue block

    The rescue block catches errors and handles them so the program doesn't crash.
  3. Final Answer:

    To handle errors and prevent the program from crashing -> Option D
  4. 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

  1. Step 1: Identify Ruby syntax for error handling

    Ruby uses begin, rescue, and end keywords for exceptions.
  2. 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.
  3. Final Answer:

    begin\n risky_code\nrescue\n handle_error\nend -> Option A
  4. 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

  1. Step 1: Identify the error in the code

    The code tries to divide 10 by 0, which raises a ZeroDivisionError.
  2. Step 2: Understand the rescue block

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

    Cannot divide by zero -> Option A
  4. 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

  1. Step 1: Analyze the operation inside begin block

    Adding 5 (number) and "3" (string) causes a TypeError in Ruby.
  2. Step 2: Check error handling

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

    You cannot add a number and a string directly -> Option C
  4. 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

  1. Step 1: Understand handling multiple errors

    Ruby allows multiple rescue clauses to catch different error types separately.
  2. Step 2: Apply multiple rescue clauses

    Write rescue clauses like rescue ZeroDivisionError and rescue IOError to handle errors distinctly.
  3. Final Answer:

    Use multiple rescue clauses with specific error classes -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard