Complete the code to start a block that handles exceptions.
begin puts 'Hello world' [1] end
The rescue keyword starts the block that catches exceptions in Ruby.
Complete the code to print an error message when an exception occurs.
begin 1 / 0 rescue => [1] puts "Error: #{e.message}" end
The variable e is commonly used to hold the exception object in a rescue block.
Fix the error in the rescue block to correctly handle exceptions.
begin file = File.open('nonexistent.txt') rescue [1] puts 'File not found' end
StandardError catches the Errno::ENOENT exception for file not found errors in Ruby.
Fill both blanks to retry the operation if an exception occurs.
begin puts 'Trying...' 1 / 0 rescue [1] => e puts e.message [2] end
ZeroDivisionError catches the division error, and retry repeats the begin block.
Fill the two blanks to ensure a message is printed whether or not an exception occurs.
begin puts 'Start' 1 / 0 rescue [1] => e puts e.message [2] puts 'Always runs' end
ZeroDivisionError handles division errors, and ensure runs code always after begin/rescue.
