原文地址:http://stackoverflow.com/questions/2191632/begin-rescue-and-ensure-in-ruby
Yes, ensure ensures that the code is always evaluated. That's why it's called ensure. So, it is equivalent to Java's and C#'s finally.
The general flow of begin/rescue/else/ensure/end looks like this:
点击(此处)折叠或打开
  1. begin
  2.   # something which might raise an exception
  3. rescue SomeExceptionClass => some_variable
  4.   # code that deals with some exception
  5. rescue SomeOtherException => some_other_variable
  6.   # code that deals with some other exception
  7. else
  8.   # code that runs only if *no* exception was raised
  9. ensure
  10.   # ensure that this code always runs, no matter what
  11. end


09-04 06:39
查看更多