[Fixed]-Writing if/else to avoid parserError saying document is empty

1👍

The proper way to catch exception is to use try/except. Something like:

def extract(url):
    g = Goose()
    try:
        article = g.extract(url=url)
        if article.top_image is None:
            return DEFAULT 

        else:
            if article.top_image.src is None:
              return DEFAULT
            else:
                resposne = {'image':article.top_image.src}
                return article.top_image.src
    except ParseError:
        handle_exception()

As for speed, no ifelse is not faster in these kind of occasions. There’s should be (almost) cost in using tryexcept as long as exception doesn’t happen (and as you pointed out it doesn’t happen often).

Even in the case where exception actually happens it isn’t very expensive. It is considered the preferred error handling mechanism in python and it’s used quite liberally in the libraries.

One thing to note when you catch an exception (regardless of language) you should actually handle the error properly. If you can’t handle the error it’s most often correct to just let it propagate up. There’s almost never proper to just swallow the exception silently.

What you could do however is to re-raise if you find out you can’t handle it, or if you just want to print out some information:

def extract(url):
    g = Goose()
    try:
        article = g.extract(url=url)
        if article.top_image is None:
            return DEFAULT 

        else:
            if article.top_image.src is None:
              return DEFAULT
            else:
                resposne = {'image':article.top_image.src}
                return article.top_image.src
    except ParseError:
        if can_handle():
            handle_exception()
        else:
            print("Oops, couldn't handle exception: url={0}".format(url))
            raise

Leave a comment