Complete the code to raise a standard error.
raise [1], "An error occurred"
Exception directly instead of StandardError.SyntaxError which is for syntax issues.The StandardError is the default for most errors in Ruby. Using raise StandardError creates a standard error.
Complete the code to rescue only runtime errors.
begin # some code rescue [1] => e puts e.message end
Exception which rescues all exceptions.StandardError which is more general.To rescue only runtime errors, use rescue RuntimeError. This catches errors of that specific type.
Fix the error in the code to correctly rescue argument errors.
begin Integer('abc') rescue [1] => e puts "Error: #{e.message}" end
RuntimeError which does not catch argument errors.TypeError which is different.The method Integer('abc') raises an ArgumentError when the string is invalid. So rescue ArgumentError to catch it.
Fill both blanks to create a custom error class inheriting from the correct superclass.
class MyError < [1] def message "Custom error message" end end raise [2], "Oops!"
Exception which is too broad.StandardError instead of the custom error.Custom errors usually inherit from StandardError. To raise the custom error, use its class name MyError.
Fill all three blanks to correctly check error class and message.
begin raise [1], "File not found" rescue [2] => e if e.is_a?([3]) puts e.message end end
ArgumentError which won't catch runtime errors.is_a?.The code raises a RuntimeError, rescues StandardError (which includes runtime errors), and checks if the error is a RuntimeError before printing the message.