Christopher Dicely
4/8/2009 12:55:00 AM
On Tue, Apr 7, 2009 at 12:34 PM, Paganoni <noway@fakenullvoid.com> wrote:
> Hello, when writing stuff like that
>
> begin
> =C2=A0call_this()
> =C2=A0do_that()
> =C2=A0tell_them()
> =C2=A0print "Ho yes #{my_dear} you did it"
> rescue SomeException =3D> e
> =C2=A0# snip
> end
>
> Should I move the print statement to the else part of begin/end catch blo=
ck
> ?
>
> begin
> =C2=A0call_this()
> =C2=A0do_that()
> =C2=A0tell_them()
> rescue SomeException =3D> e
> =C2=A0# snip
> else
> =C2=A0print "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 =
?
Yes, often, because you should (in general) only be rescuing
exceptions if you can do something useful with them where you are,
otherwise, you should be letting them bubble up. So if the errors you
are going to deal with aren't the one's in the print line, it makes
sense to have it in the else. Of course, if you different logic to
deal with errors in the print statement, it even make make sense to
do:
begin
.
.
.
rescue SomeException =3D> e
.
.
.
else
begin
print "Ho yes #{my_dear} you did it"
rescue SomeException =3D> e
.
.
.
end
end