From: Joel VanderWerf Date: 2009-04-08T05:13:08+09:00 Subject: Re: Exceptions : should I else or not ? Paganoni wrote: > Hello, when writing stuff like that > > begin > call_this() > do_that() > tell_them() > print "Ho yes #{my_dear} you did it" > rescue SomeException => e > # snip > end > > Should I move the print statement to the else part of begin/end catch > block ? > > begin > call_this() > do_that() > tell_them() > rescue SomeException => e > # snip > else > print "Ho yes #{my_dear} you did it" > end > > I presume that the result is the same, but for clarity I would prefer > the second solution - Now, what happen if the print statement fails or > if my_dear() raises something ? I'll not catch it losing the benefit of > the begin/end. So, even if easier to read, is else really useful/a good > idea ? The second way gives you more control over where exceptions are caught, and therefore more precise knowledge of the source of the exception. As you point out, the print statement might raise something. If that happens above the rescue clause, then you don't necessarily know what the source of the exception was. This is particularly important for common errors like NoMethodError, which can be caused by typos as well as by situations that are expected by the program. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407