Search code examples
pythonerror-handlingpraw

Handling Errors in PRAW (Reddit)


I'm playing around with making an auto reply bot that scanns r/All submission comments for a given command eg. !command

When it detects this command it replies to the comment with a string:

eg. "Hello"

The error I get is that there is a limit on new accounts where they can only comment once every 10 minutes. And when the bot comments and moves to the next comment to reply to it gets this error:

raise APIException(*errors[0])
praw.exceptions.APIException: RATELIMIT: 'you are doing that too much. 
try again in 2 minutes.' on field 'ratelimit'

How can I detect this type of error so the code knows what to do, at the moment the whole script that stops and I have to run it again after 10 minutes.

Had a read of https://praw.readthedocs.io/en/latest/code_overview/exceptions.html but still not getting it


Solution

  • An Exception is raised here, which you can handle using a try..except

    try:
        the_api_call()
    
    except APIException as e:
        print "Handling exception like a baus"
    

    P.S. you would need to import APIException as it's not a in-built exception.