Returning to 'try' block after catching an exception

inhahe inhahe at gmail.com
Thu May 22 14:01:04 EDT 2008


Might have a stack overflow issue, if it retries too many times?

"alex23" <wuwei23 at gmail.com> wrote in message 
news:b3680690-73c3-47cb-8693-54a207daafcd at x1g2000prh.googlegroups.com...
> On May 22, 6:15 pm, Karlo Lozovina <_karlo_ at _mosor.net_> wrote:
>> Because when you expect exception to occur on something like 0.01% of
>> cases, and you have 4 or 5 exceptions and the code to test for each
>> conditions that cause exceptions is quite long and burried deep inside
>> some other code it's much better to do it this way ;). Too bad there's no
>> syntactic sugar for doing this kind of try-except loop.
>
> I'm surprised your unit tests let it get to such a state... ;)
>
> How about something like this?
>
>    retry, total_fail = False, False
>    try:
>        some_function()
>    except SomeException:
>        some_function2()
>        some_function3()
>        retry = True
>    finally:
>        if retry:
>            try:
>                some_function()
>            except SomeException:
>                total_fail = True
>
> Using 'finally' seems more explicit about it being part of the
> exception handling than using a loop construct.
>
> Actually, this is even more direct:
>
>    try:
>        some_function()
>    except SomeException:
>        some_function2()
>        some_function3()
>        try:
>            some_function()
>        except SomeException:
>            raise SomeError 





More information about the Python-list mailing list