Skip to content

Never leave the house with a bare exception

July 24, 2013

Some times best practices end up being just for aesthetics, and some times best practices exist so you aren’t stuck in an infinite loop which is catching your KeyboardInterrupt’s. There is basically no excuse to ever use code that looks like this:


If you don’t believe me, try this code:

while True:

Helpful hint: ctrl-\ will crash your interpreter. Which is the only way to get out of that loop. Today, I interacted with some code which is the moral equivalent of above. Except for that the infinite loop seemed to involve looping through a series of about five files. Those files were littered with bare except clauses. Finding the block catching my KeyboardInterrupt’s was effectively impossible. Just don’t do it.

If you insist on catching a plethora of exceptions try:

while True:
  except Exception:

That code will at least let you escape with a KeyboardInterrupt. If you are using a package which throws an exception not inheriting from Exception, I suggest you either write wrappers to catch the package-defined exceptions, or just use a better package.


From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: