Ruby rescue all exceptions except MyException

在Ruby中,是否有可能拯救除指定之外的所有异常?


Not as such no. But you can rescue all exceptions and reraise the exception if it is a MyException.


begin

rescue MyException
  raise #to reraise the same exception without changing it
rescue Exception => e
  #do something with e
end

Without knowing more about your problem, I'd suggest Ken Bloom's answer.

However, I'd like to know more about why you're doing it.

Are you worried about a really severe exception, and not wanting to rescue that one, but allow less severe exceptions to be rescued?

In that case, I'd make my custom exception inherit from Exception rather than StandardError and then have

begin
  do_risky_stuff
rescue # Not rescue Exception
  handle_less_serious_stuff
end
链接地址: http://www.djcxy.com/p/25848.html

上一篇: Rails异常处理

下一篇: Ruby除了MyException之外拯救所有异常