I don’t understand why this code is not working properly:
def test
raise Exception.new 'error'
end
def caller
begin
test
rescue =>e
puts e.message
end
end
caller
I want to catch the test exception in the caller method, but it seems that the caller method is not catching anything.
Jan beat me to it, but…
When you use the
=> varsyntax withexception, you must specify what kind of exception you want to rescue. The base class for all exceptions is Exception, so it will work if you change that torescue Exception => e. Also, when what you’re rescuing from is the entire body of a method, you don’t need an explicit begin…end block…