From: Gennady Date: 2003-06-03T00:38:52+09:00 Subject: Re: Return from ensure ----- Original Message ----- From: "Robert Klemme" Newsgroups: comp.lang.ruby To: "ruby-talk ML" Sent: Monday, June 02, 2003 1:23 AM Subject: Re: Return from ensure > > "Gennady" schrieb im Newsbeitrag > news:001101c32624$5aef3490$c20ca8c0@gfb... > > Hi, Rubyists > > > > Actually, I saw a remark on ruby-talk once that "return" is dagerous, > > however I could not imagine it is that dangerous. Today I spent 2 hours > > debugging the code that looked perfectly normal otherwise. I was able > reduce > > the code to the following fragment: > > > > def test > > begin > > raise "ERROR" > > ensure > > return "some value" > > end > > end > > > > test > > puts "Nothing happened" > > > > > > Yes, it prints out "Nothing happened", and if you comment out "return" > you > > will see the exception. I just wanted to provide a return value for the > > function here, so I naturally used "return" statement. I know that in > Ruby > > you can return something without explicit "return" and I usually do this > > every time. However, at that unfortunate moment my instinct injected > > "return" -- and, here we go, 2 hours of fun. > > > > Any comments? > > "ensure" is inappropriate for catching exceptions and returning something > else. You must use "rescue" for this. The purpose of "ensure" is to have > a piece of code that is invoked regardless of how the block is exited. > This code however should only do cleanup and not modification of the exit > procedure - as you have experienced. > > You'll find this very nicely documented in the pickaxe chapter > "Exceptions, Catch, and Throw". > I understand perfectly well that I should not have done this. And in fact I refactored the code so there's no return from ensure. However, my point was that it should have been handled by Ruby better, either ignoring return in the presence of an exception and going on with whatever current exception handling were in place, or at least give some kind of a warning at compile time. Or simply advertising it as a language feature, what seems to me now as a good idea. Actually my initial intent was preserving the data collected by the class that raised the exception so far and retruning a value if everything was OK. The exception was supposed to be handled by some other class that would adjust input criteria accordingly and continue. By the way, I re-read the pickaxe chapter you reffer to, very nice and informative one indeed. However there's nothing there on the issue I have raised, no "gotcha's" on the fact that return from ensure in practice means ignoring all exceptions. Now that I know this :-) I am not sure if it is a side effect or the guarateed language behaviour. Thanks for replying, Robert. I did not get many responces on this, surprisingly. P.S. Hmm, I just tested that return from "finally" in Java also means ignoring exceptions. Interesting, I would never have thought. So it can be regarded as a guaranteed feature after all. > Regards > > robert > > >