“Gennady” gfb@tonesoft.com schrieb im Newsbeitrag
news:000901c3291d$154a09f0$c20ca8c0@gfb…
I understand perfectly well that I should not have done this.
Sorry, in that case I did not understand you correctly.
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.
Yes. Although I feel a bit wary about this, because for catching and
returning we still have ensure. IMHO it should not be allowed to return
from an ensure or have the interpreter warn about this.
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.
But you would to this in an rescue clause. If all is well, return the
value otherwise rethrow by simple writing “raise” on a line. (See the
example in the referenced chapter.)
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.
Yes indeed, that’s true. Maybe others have thought it so evident that you
typically won’t return from an ensure section so they did not see it
necessary to write a sentence about this.
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.
Um, well, often error handling is neglected… :-}
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.
Yes, I checked with the JLS. It’s a bit scattered: in 14.1 they define an
abrupt completion as one of break, continue, return or throw. In 14.19.2
they say that if a finally clause completes abruptly for some reason S
then the whole try clause terminates for this reason S. In our case S ==
“return”.
14.1
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#5894
14.19.2
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#236653
Cheers
robert