From: Stefan Lang Date: 2008-10-02T20:59:24+09:00 Subject: Re: "ensure" hiding actual error 2008/10/1 Nit Khair : > Glen Holcomb wrote: >> On Wed, Oct 1, 2008 at 9:08 AM, Nit Khair wrote: >> >>> >> problems with handling correctly the original exception. >>> ensure >>> my_form.free_form. >>> ... >>> end >>> >> >> What happens here if my_form never got a value because there was an >> error >> previous that made create_... invalid? >> >> What Thomas is saying is that there is an exception being raised by the >> code >> present in your ensure block. >> >> > Fine. I get that. So is there a better or correct way of doing this so > that I get the original error that was raised. > > Aaaah. so you mean if i check for my_form before freeing, then ... since > no new exception is raised, the original exception will be displayed. > So I _should_ say: Generally, the right way is to put the initialization _before_ the begin/ensure block: resource = init_resource begin # do something with resource ensure resource.close end If all you do in the ensure block is to clean up the resource, it is useless to put the resource initialization in the begin/ensure block, because if the initialization fails, there's nothing to cleanup anyway. Stefan