The Ruby keyword for catching exceptions.
rescue
is used in Ruby to catch exceptions that have been thrown. The general exception handling structure is:
begin
#...
rescue E1 => e
#... handle E1
rescue E2 => e
#... handle E2
else
#... No exceptions
ensure
# ... Always executed.
end
You can leave out the begin
inside a method:
def m
#...
rescue
#...
end
Or use it as a statement modifier
x = blah_blah rescue 11