Complete the code to rescue a ZeroDivisionError.
begin result = 10 / 0 rescue [1] puts "Cannot divide by zero" end
The ZeroDivisionError is raised when dividing by zero. Rescuing it handles this specific error.
Complete the code to rescue an IOError.
begin file = File.open(__FILE__) file.close file.read rescue [1] puts "IO operation failed" end
IOError is raised for input/output errors like attempting to read from a closed file.
Fix the error in the rescue clause to handle both ZeroDivisionError and IOError.
begin # some code that may raise errors rescue [1], IOError puts "Handled an error" end
To rescue multiple errors, list them separated by commas. Here, ZeroDivisionError and IOError are rescued.
Fill both blanks to rescue ArgumentError and NoMethodError separately.
begin # risky code rescue [1] puts "Argument error caught" rescue [2] puts "No method error caught" end
Each rescue clause handles a specific error: ArgumentError and NoMethodError.
Fill all three blanks to rescue RuntimeError, TypeError, and StandardError in one rescue clause.
begin # code that may raise errors rescue [1], [2], [3] puts "An error was rescued" end
Multiple errors can be rescued by listing them separated by commas in one rescue clause.