0
0
Rubyprogramming~5 mins

Begin/rescue/end blocks in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
Which keyword is used to catch exceptions in Ruby?
Aexcept
Bcatch
Crescue
Dhandle
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
Which block runs code regardless of whether an exception occurs or not?
Arescue
Bensure
Cfinally
Dbegin
How do you rescue multiple specific exceptions in one rescue clause?
Arescue Exception1, Exception2
Brescue Exception1 | Exception2
Crescue (Exception1, Exception2)
Drescue Exception1 & Exception2
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.