Try Except Pass Philosophy

Try: Except: Pass. Three words that can tell you a lot about a developer. It is often used as in in joke for python developers, but every now and then you see it being used out in the wild. It is the mark of some truly poor coding practices.

I want to keep this blog as beginner friendly as possible. Python has a great community and is always committed to encouraging new people to learn. Python tends to be very forgiving when it comes to making mistakes, making it a really good first language. If you ever see try: except: pass in code, it is always a sign that someone is experimenting and learning something new (or that they are incredibly pushed for time, has not fully tested the code, and wants something that won’t look like it’s not working).

Now for the lesson

For the beginners out there, a try except block is used to “try” and execute some code. If it fails and raises an “except"ion then the exception code is executed. Usually this code either provides more debug information to the programmer, or try and fix the problem, or might just be used as the general control flow of your program. Typical usage looks like this:

Try :
  Some questionable code:
  Print('questionable code failed to run')

Here, when questionable code raises an exception, the except code gets executed. As for the “pass”, it is a keyword to tell python that you have declared a code block but haven’t put any code in there, you need to explicitly tell python to do nothing. Now if you were a "good" programmer, or developing "good" code, you would never ever just do nothing. Carrying on, and leaving a message is at least telling the user that something happened, but doing NOTHING is probably the worst thing you can do.

Now this happens in a lot of different programming languages, but I think due to the lower entry level of Python, and the uptick of people learning it, means that you see a lot more of it around. Personally I believe it is a beautiful construct. It embodies a lot of what python is about; getting your code up and running.

Fun stuff

"pip install fuckit" I don’t know how I came across this library, but it is pretty fantastic at what it does. I don’t know enough about Python (always more to learn!) to fully understand what it is doing, but I love it to bits. From my testing, it looks like it puts a try:except:pass around every single line of code. No more errors EVER!