Wednesday, September 21, 2011

[Level 2] Testing combination of loop and try.

Sample code and test result are as the following:
#!/bin/env python
class e(Exception):
    def __init__(self):
       pass

server_list = [1,2,3,4,5]
for server in server_list:
    try:
        sReturn = server
        print 's:%s' % sReturn
        if sReturn<3: continue
        if sReturn==3: raise e
        print 'try:%s' % sReturn
    except Exception, e:
        print 'except1:%s' % sReturn
        continue
        print 'except2:%s' % sReturn
    else:
        print 'else:%s' % sReturn
        break
    finally:
        print 'finally:%s' % sReturn
$ ./t.py 
s:1
finally:1
s:2
finally:2
s:3
except1:3
finally:3
s:4
try:4
else:4
finally:4
Wish this helps.
regards,
Stanley Huang