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?
✗ Incorrect
In Ruby,
begin starts a block where exceptions can be handled with rescue.Which keyword is used to catch exceptions in Ruby?
✗ Incorrect
rescue is used in Ruby to catch and handle exceptions.What will happen if an exception is raised but no
rescue block matches it?✗ Incorrect
If no matching
rescue block is found, Ruby raises the exception and the program stops.Which block runs code regardless of whether an exception occurs or not?
✗ Incorrect
ensure runs code no matter what, useful for cleanup.How do you rescue multiple specific exceptions in one
rescue clause?✗ Incorrect
You list exceptions separated by commas after
rescue to catch multiple types.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.