Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to ensure the cleanup message is always printed.
Ruby
begin puts 'Start' [1] end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using rescue instead of ensure
Using else instead of ensure
Using finally which is not Ruby syntax
✗ Incorrect
The ensure block always runs, even if an error occurs.
2fill in blank
mediumComplete the code to print 'Cleanup done' regardless of errors.
Ruby
begin puts 'Processing' raise 'Error occurred' [1] end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using rescue to print cleanup message
Using else which runs only if no error
Using finally which is not valid Ruby
✗ Incorrect
The ensure block runs no matter what, so 'Cleanup done' is always printed.
3fill in blank
hardFix the error in the code to ensure cleanup always runs.
Ruby
begin puts 'Running' [1] puts 'Done' end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using rescue which runs only on errors
Using else which runs only if no error
Using finally which is not Ruby syntax
✗ Incorrect
Only ensure guarantees the cleanup code runs regardless of errors.
4fill in blank
hardFill both blanks to print 'Start', handle errors, and always print 'Cleanup'.
Ruby
begin puts 'Start' raise 'Oops' [1] [2] end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using else instead of rescue
Using finally which is invalid
Swapping rescue and ensure order
✗ Incorrect
rescue handles errors, ensure always runs for cleanup.
5fill in blank
hardFill all three blanks to handle errors, print success message, and always cleanup.
Ruby
begin puts 'Try block' raise 'Fail' [1] [2] [3] end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using finally which is invalid
Omitting else block
Placing ensure before rescue
✗ Incorrect
rescue catches errors, else runs if no error, ensure always runs for cleanup.