From: Wilson Bilkovich Date: 2006-12-12T01:05:03+09:00 Subject: Re: else inside rescue is useless? On 12/11/06, cap wrote: > http://www.awprofessional.com/bookstore/product.asp?isbn=0672328844&rl=1 > > The "The ruby way" book said > > [quote] > In the event that error types are specified, it may be that an > exception does not match any of these types. For that situation, we are > allowed to use an else clause after all the rescue clauses. > > begin > # Error-prone code... > rescue Type1 > # ... > rescue Type2 > # ... > else > # Other exceptions... > end > > [/quote] > > but actually, I found the "else" clause is only executed when there is > no exception been thrown,such as : > > ------------------------- > begin > puts "do something" > rescue NameError > puts "name error #{$!}" > else > puts "else #{$!}" > end > -------------------------- > > the result is > > ------------------- > do something > else > ------------------ > > so the "else" clause is useless because we can simply put the content > of "else" caluse to the last line in the "begin" block before the first > "rescue" > > Am I wrong or the book is wrong? > > my ruby version is > ruby 1.8.4 (2005-12-24) [i686-linux] > > > From the Rubinius test suite: @count = 1 begin p @count raise ArgumentError, 'just kidding' unless @count > 3 rescue Exception => e @count += 1 retry else p 7 ensure p 8 end this prints: 1 2 3 4 7 8 'else' only runs if no exception was raised.