control structures (was "Re: Sins")

William Tanksley wtanksle at hawking.armored.net
Thu Jan 6 19:43:07 EST 2000


On Wed, 5 Jan 2000 15:22:56 -0800, Emile van Sebille wrote:
>Doesn't this do that now? or is there some objection to this usage?

>for i in range(3):
>  try:
>    elem = "... nested loops & stuff ..."
>    if i < 2:
>      raise "element found", elem
>    "... more stuff ..."
>    raise "search failed"
>  except "element found", e:
>    print "Found", e

I suggested this as an alternative to using the truly detestable "else"
for "for" loops, and the reason to not use exceptions that way was that
exceptions can accidentally travel further than you intend; if you forget
to catch, or accidentally catch the wrong thing, the exception will
continue travelling up the call stack.  Eventually it might get caught by
some other function you wrote which was using the same exception but will
recieve your exception instead of its own.

In addition, your code won't work -- all strings are treated as the same
type of object for the purpose of exceptions, according to the people who
rebuked me.  If that's true, your except clause would catch both
exceptions.

At any rate, labelled breaks would be a nice thing to have.

>Emile van Sebille

-- 
-William "Billy" Tanksley, in hoc signo hack



More information about the Python-list mailing list