An exception is not thrown inside the function, but in the place where it is called, so you need to catch it somewhere else:
def divide(a, b) a.to_s + ' divided by ' + b.to_s + ' is ' + (a/b).to_s end begin divide(4) rescue ArgumentError puts 'there must be two arguments' end
While this works, catching an ArgumentError
is a very bad idea, as it indicates an error in the code that you cannot recover from.
source share