Bird
Raised Fist0
Rubyprogramming~5 mins

Multiple rescue clauses in Ruby - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of multiple rescue clauses in Ruby?
Multiple rescue clauses allow you to handle different types of errors separately, giving specific responses for each error type.
Click to reveal answer
beginner
How do you write multiple rescue clauses in Ruby?
You write multiple rescue clauses by listing different rescue blocks one after another, each specifying a different error class to catch.
Click to reveal answer
intermediate
What happens if an exception does not match any rescue clause?
If an exception does not match any rescue clause, it will propagate up the call stack and may cause the program to terminate if not handled elsewhere.
Click to reveal answer
beginner
Example: What will this code output?
begin
  1 / 0
rescue ZeroDivisionError
  puts 'Cannot divide by zero'
rescue StandardError
  puts 'Some other error'
end
The output will be: Cannot divide by zero Because the error is ZeroDivisionError, the first rescue clause handles it.
Click to reveal answer
intermediate
Can you rescue multiple exceptions in a single rescue clause?
Yes, you can rescue multiple exceptions by listing them separated by commas after rescue, like: rescue IOError, EOFError.
Click to reveal answer
What keyword is used to handle exceptions in Ruby?
Atry
Bcatch
Crescue
Dexcept
If you want to handle both IOError and EOFError in the same rescue clause, how do you write it?
Arescue IOError | EOFError
Brescue IOError, EOFError
Crescue (IOError, EOFError)
Drescue IOError && EOFError
What happens if an exception is not rescued in any rescue clause?
AThe program crashes or the exception propagates up
BThe exception is ignored silently
CThe program restarts automatically
DThe exception is converted to a warning
Which rescue clause will handle a ZeroDivisionError if multiple rescue clauses exist?
AThe first rescue clause that matches ZeroDivisionError
BThe last rescue clause
CAll rescue clauses
DNone, ZeroDivisionError is not rescued
Can you have an else clause after multiple rescue clauses?
ANo, else is not allowed
BOnly if you have a finally clause
CYes, but it runs only if an exception occurs
DYes, it runs if no exception occurs
Explain how multiple rescue clauses work in Ruby and why they are useful.
Think about catching different problems separately.
You got /3 concepts.
    Write a Ruby code snippet that rescues both ZeroDivisionError and TypeError with different messages.
    Use separate rescue clauses for each error.
    You got /3 concepts.

      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