From: Robert Klemme Date: 2008-10-06T01:13:28+09:00 Subject: Re: "ensure" hiding actual error On 02.10.2008 14:48, Nit Khair wrote: > Dave Thomas wrote: >> On Oct 2, 2008, at 7:39 AM, Nit Khair wrote: >> >>> I spent many years in Java where the style was: >>> >>> try { >>> resource = init_resource >>> } catch (exception) {} >>> finally { >>> if resource != null { resource.close } >>> } >> This is common in Java, but it is incorrect. >> >> What happens if init_resource() throws an exception? > > Oops I forgot to mention that in finally {} one would check for resource > not being nil. Corrected above. The pattern is still broken - or put it differently: overly complex. This is easier and as safe - and you can make the resource final and avoid accidental reassignment: final Resource res = initResource(); try { res.use(); res.use(); res.use(); res.use(); } finally { res.cleanup(); } Cheers robert