Hey, it’s me again. I have been away for a while but I have certainly not been idle.
Today I want to talk about a programming concept that has recently become completely clear to me. Namely, try catch.
Don’t get me wrong, I have known the concept for some time and I was aware that this was a real fact in the programming world, it was only after writing a python script that I fully understand all the benefits (I think).
The first programming language I learned was PHP and so I came into contact with this concept within the PHP language for the first time. If I remember correctly, I watched a video that explained the concept, after which I read a few more articles.
The idea I had at the time was. “Ah, so this is a way to program feedback in the system. Developers send a message to other developers (consumers) of their system, as it were, when these consumers somehow make a mistake“.
In practice I saw this method often used in the Laravel framework. Not so much in WordPress. Although my opinion can be quite biased as I only work with these two frameworks frequently.
There is a second use case to this feature that was unknown to me till this day. To explain this, I first have to give a little background information.
I’ve been interested in the stock market for a long time, and everything related to it, options, stocks, crypto.
I recently came across PartTimeLarry’s YouTube channel, he uses python for stock market analysis and to make trading bots, which I think is pretty awesome.
His videos gave me the idea to write a python script myself, the general gist was: I would request Google trends data for certain stock tickers, after which I will apply certain formulas to this data to spot certain trends in stocks.
I didn’t know much about Python yet, but after one Pluralsight course and a few videos on YouTube I managed to create a working script.
As soon as I ran the script, I left the computer for a while, usually it took about 5 minutes to request 3000 keywords from Google. This has to do with the fact that you can only request 5 keywords per API request. This is where I ran into a problem … Every so often an “Exception” was thrown. The reason could vary, 429 to many requests, 500 server error, 408 request timeout. In turn I had to manually restart the script, very annoying.
So I started looking for a solution and that is how I came to the second use case of try catch blocks. By using try catch (called “Except blocks” in Python) you can influence the operation of a script. If you catch certain Exceptions, you can tell the script what to do next. In this way I was able to program all kinds of Exceptions and let the script continue to run. There are of course certain Exceptions after which you cannot continue, for example “429, to many requests”.
Below you can see an example of the Exceptions that I receive in my script:
Every time you see “exit” or “pass” the script will stop, but if the script enters an Exception that says “continue” the script will continue as usual.
except pytrends.exceptions.ResponseError as e: status_code = e.response.status_code if(status_code == 429): exit("We've made to many requests, we shall continue tomorrow.") except requests.exceptions.Timeout: print("Timeout occured") continue except ssl.SSLEOFError: print('SSL error occured') continue except requests.exceptions.SSLError: print('Deeper SSL error occured') continue except ValueError as e: print(e) pass
That’s pretty much the latest piece of news I’ve discovered recently. In the future I will also write an article where I will explain the entire operation of my Google trends script, if this interests you, stay tuned!