Method Call in Exception

bruno at modulix onurb at xiludom.gro
Thu Apr 20 05:26:23 EDT 2006


mwt wrote:
(snip)
>>>This works when I try it, but I feel vaguely uneasy about putting
>>>method calls in exception blocks.
>>
>>What do you put in exception blocks?!

Whatever fits the specific case...

(snip)
> Normally I don't like to use exception blocks to do condition-statement
> stuff. At least that is how my Java training has programmed me.

http://dirtsimple.org/2004/12/python-is-not-java.html

> But in
> this case, the condition is whether the internet connection is working
> or not, and I don't see any other way to do it.

As Felipe wrote, you could also use a if/else.


> And furthermore, I
> don't know if the exceptions-as-logic is a no-no in Python.

It's not.

The main guideline here is that a else/if has a constant cost - the test
is always executed -, while a try/except only adds overhead if it's
fired. So - if you leave out purely stylistic or religious
considerations - the 'good' choice depends mostly on the 'cost of the
test'/'cost of the exception' ratio and the 'have connection'/'no
connection' ratio.

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list