Handling a timeout exception in Python
<p>I'm looking for a way to handle timeout exceptions for my Reddit bot which uses PRAW (Python). It times out at least once every day, and it has a variable coded in so I have to update the variable and then manually run the bot again. I am looking for a way to automatically handle these exceptions. I looked into try: and except:, but I am afraid that adding a break point after time.sleep(10) would stop the loop completely. I want it to keep running the loop regardless if it times out or not. There is a sample of the code below.</p>

<pre><code>def run_bot():
   # Arbitrary Bot Code Here

   # This is at the bottom of the code, and it runs the above arbitrary code every 10 seconds
while True:
    try:
        run_bot()
        time.sleep(10)
    except:
        # Don't know what goes here
</code></pre>

<p>Moving the sleep to finally will solve your issue, I guess. finally block will run irrespective of whether there is an exception happened or not.</p>

<pre><code>def run_bot():
   # Arbitrary Bot Code Here

   # This is at the bottom of the code, and it runs the above arbitrary code every 10 seconds
while True:
    try:
        run_bot()
    except:
        from traceback import format_exc
        print "Exception happened:\n%s" % (format_exc())
    finally:
        time.sleep(10)
</code></pre>

python exception timeout handle praw