Rescuing Exceptions Inside Methods

http://www.skorks.com/2009/09/ruby-exceptions-and-exception-handling/

Rescuing Exceptions Inside Methods

If we are inside a method and we want to rescue some exceptions, we don’t actually need a begin .. end block since the method definition itself will act in that capacity. So, we can do something like this:

def some_method
  p 'Hello method'
  raise
  p 'Bye method'
rescue
  p 'Rescuing exceptions'
end
some_method

which print out:

"Hello method"
"Rescuing exceptions"

We have rescued an exceptions without having a begin .. end block.

def  xxx

....

rescue OpenURI::HTTPError, OpenURI::HTTPRedirect
  false
end
原文地址:https://www.cnblogs.com/iwangzheng/p/3797828.html