How to get out of a try/except inside a while? [Python]

前端 未结 4 738
野趣味
野趣味 2021-02-14 16:12

I\'m trying this simple code, but the damn break doesn\'t work... what is wrong?

while True:
    for proxy in proxylist:
        try:
            h = urllib.urlo         


        
4条回答
  •  一整个雨季
    2021-02-14 16:52

    You can use a custom exception and then catch it:

    exit_condition = False
    
    try:
    
        
    
        if exit_conditon is True:
            raise UnboundLocalError('My exit condition was met. Leaving try block')
    
        
    
    except UnboundLocalError, e:
        print 'Here I got out of try with message %s' % e.message
        pass
    
    except Exception, e:
        print 'Here is my initial exception'
    
    finally:
        print 'Here I do finally only if I want to'
    

提交回复
热议问题