Method Call in Exception

Serge Orlov Serge.Orlov at gmail.com
Wed Apr 19 21:16:08 EDT 2006


Felipe Almeida Lessa wrote:
> Em Qua, 2006-04-19 às 16:54 -0700, mwt escreveu:
> > 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?!
>
>
> > So tell me, Brave Pythoneers, is this
> > evil sorcery that I will end up regretting, or is it just plain good
> > ol' Python magic?
>
> IMHO, the exception block in Python is used a lot in places where you
> could use an if-then-else, like your example that could be written as
>
>     if internet_available():
>         [...] #doing some internet stuff
>     else:
>         alternate_method_that_doesnt_need_internet()
>
> So yes, I think there's no problem there.

What if the service is overloaded and always times out? For end user
it's basically the same as there is no internet access. I routinely use
the following pattern:

unrecoverable = MemoryError, IOError
try:
    do_your_best()
except unrecoverable, e:
    util.help_on_error(e)
    sys.exit(1)

def do_your_best():
    recoverable = IOError, socket.error, SomeOtherError
    try:
         do_something()
    except recoverable, e:
         do_something_else()




More information about the Python-list mailing list