Raising an exception to the sole purpose of catching it?

He is presenting a concept, not a useful implementation, so don't stare at that particular code too much.

The point is, you should handle all errors appropriately. What that means is your call. If you don't care if something fails, then just ignoring it and printing the error is totally fine (as rare as that is), and catching and ignoring the error is the only way to...ignore an error.

A real world example of this might be a program that periodically downloads new data from the internet every 5 minutes.

while True:
    try:
        download_new_data()
        print "Data downloaded."
    except Exception as e:
        print("Couldn't download data:", e)
    sleep 5*60

If my internet connection goes down, I don't want the program to crash. Since I know it will try again, it's appropriate to just ignore the error.

And, why not put the check into download_new_data()? This is where the separation of concerns is important. The purpose of download_new_data() should be just to download the data. If I want to reuse the function in another script where I do care about its success, I couldn't if the "catch all" error handling was inside download_new_data(). Instead, I would probably make a new function called try_download_new_data() that had a catch all, or add a parameter if errors should be ignored.

/r/learnpython Thread Parent