How to continue the execution after catching an exception?

advertisements

I am using twython to get some tweets from twitter. To get the tweets I use try / except, like

for follower in followers_file_id:
    follower = follower.strip()
    try:
      if req_user_settings > max_user_settings_req or req_user_tweets > max_tweets_req:
        time.sleep(15 * 60)
        tweets_file_name = tweetsFileName(followed_user)

      user_settings = twitter.show_user(user_id = follower)
      req_user_settings += 1

      if not user_settings['protected'] and user_settings['statuses_count'] > 0:
        tweets = twitter.get_user_timeline(user_id = follower, count = 200, page = 1)
        req_user_tweets += 1

      n_tweets =tweets[0]['user']['statuses_count']
      n_loops = n_tweets // max_tweets_per_page + 1
      if n_loops > max_tweet_pages:
        n_loops = max_tweet_pages

      if tweets[0]['user']['lang'] == 'en':
        n_crawled_tweets = writeTweets(tweets, tweets_file_name, n_crawled_tweets)

      for i_loop in range(1, n_loops):
        tweets = twitter.get_user_timeline(user_id = follower, count = 200, page = i_loop+1)
        n_crawled_tweets = writeTweets(tweets, tweets_file_name, n_crawled_tweets)
        req_user_tweets += 1

    except TwythonError as e:
      print str(e)

How can I tell the program to continue the execution after printing the error? Cause now after printing the error, the program stops.


The whole try block will be left as soon as an exception occurs. If you'd like to continue after exception, you need to catch the exception earlier

like

Try:
   some code here
except TwythonError as e:
   print e

some more code here