0

i want to exec a test function many times when it found a exception like this

    try:
        test()
    except:
        try:
            test()
        except:
            try:
                test()
            except Exception,e:
                logger.info(e)

this case will when i request a url and get the url response
because the Target web server may response some bad data when you just request once,
it that so many word to do this ,not a pythonic implementation
is there some pythonic style to do this?

timger
  • 944
  • 2
  • 13
  • 31
  • Never use bare except, always explicitly state exception type. – utdemir Aug 19 '11 at 09:31
  • possible duplicate of [is there a pythonic way to try something up to a maximum number of times?](http://stackoverflow.com/questions/567622/is-there-a-pythonic-way-to-try-something-up-to-a-maximum-number-of-times) – Zach Kelling Aug 19 '11 at 09:31
  • @utdemir thank you point out this i always forget it – timger Aug 19 '11 at 09:39

4 Answers4

7
for _ in range(5):
    try:
        test()
    except SomeError:
        pass
    else:
        break
else:
    #All tries failed, do something.
    logger.info(...)
utdemir
  • 26,532
  • 10
  • 62
  • 81
  • improves on Constantinius' answer in only looping if an exception occurs, and in not capturing the iteration count (using ``_``), but you could make this more efficient (though it's not that important on for such a trivial size) by using ``xrange`` -- ``range`` would actually instantiate an actual list, rather than an iterator, and I don't know if Python actually optimizes it if it notices you're not assigning it to anything – michel-slm Aug 19 '11 at 09:42
  • this will exec the test() five times whatever? – timger Aug 19 '11 at 09:43
  • @timger,this tries to exec test() five times, if test() fails everytime, the else clause executed. But if test() doesn't raise SomeError, it breaks the loop, skips the else part and continues executing. – utdemir Aug 19 '11 at 09:51
  • 1
    @hircus, in Python 3, range returns an iterable like xrange in Python2, so I generally use just range. And Python doesn't optimize it, _ don't have any special meaning, it is just a variable name. – utdemir Aug 19 '11 at 09:51
  • @utdemir the last "else" can't compile with "for" – timger Aug 19 '11 at 10:06
  • @timger, didn't understand last else part. else in for loop executes when for loop ended without break statement. The code should run well with all Python versions. – utdemir Aug 19 '11 at 10:36
  • @timger let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2660/discussion-between-utdemir-and-timger) – utdemir Aug 19 '11 at 10:36
1

Try this:

for i in xrange(3):
    try:
        test()
    except Exception, e:
        if i == 3:
           # handle exception
           # ...
Constantinius
  • 34,183
  • 8
  • 77
  • 85
1
while True:
    try:
        test()
        break
    except Exception, e:
        logger.info(e)

Have a look at the Python docs on Handling Exceptions.

Jacob
  • 41,721
  • 6
  • 79
  • 81
1
for i in range(3):
    try:
        test()
    except Exception, e:
        if i==2:
            logger.info(e)
Torsten Engelbrecht
  • 13,318
  • 4
  • 46
  • 48