-1

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.

Adham
  • 342
  • 1
  • 2
  • 13

2 Answers2

1

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
Regenschein
  • 1,514
  • 1
  • 13
  • 26
1
try:
  some code here
except TwythonError as e:
  print e
finally:
   #continue

Bear in mind that the code will always go through the finally section, therefore, you only execute the code that is throwing the exception, inside the try section and the rest in the finally section.

Jegan
  • 1,227
  • 9
  • 16