Do Python programmers just not care about handling errors? I’m using Python for a new project and am completely frustrated with the state of exceptions in Python. Python does so many things well. A simple thing like docstrings is brilliant and makes Python code much more useful, especially to those of us still somewhat new to the language (yes, we exist). But exceptions? They’re a big void, a black hole sucking away my time and sanity as I paw through pages and pages of documentation trying to find which ones are thrown for a given module or function.
Let me give you an example. I’m using SQLAlchemy for a project. I’m impressed with SQLAlchemy, it seems well designed. And the documentation seems ok, too. But, no. Let’s look at the query method. Quick, can you tell me what exceptions it throws? Well, if you hunt around a lot more, you eventually find this list of exceptions. But that doesn’t really tell me which ones query() can throw, and it doesn’t say that it’s an exhaustive list. Now look at all the code examples for SQLAlchemy. Any exception handling? I certainly haven’t found any. I guess errors never happen when using SQLAlchemy.
Let’s contrast this with the MySQL C API. Look at the page for query. I know exactly what errors occur and why for that function. Now let’s look at an example of how to use the C API. I picked a random example from a quick Google search. See that? All that sexy error checking!
Look, I know, documentation sucks. For every language and every library. It sucks to write, it sucks to maintain. And I don’t want to pick on just SQLAlchemy. Like I said, I like that project. But as I look through the documentation and example code for various Python modules, I want to scream. Apparently, people documenting Python code are pathologically incapable of explaining which exceptions get thrown and when. It’s like they live with the unicorns and LOGO programmers in the magic land of NoError ruled by King ICantProgram.
“Ok, so, yes, the documentation sucks, random self important blogger, but that’s not Python’s fault.” Wrong! They could have done something like docstrings, create a convention where it’d be easy to pull out which exceptions get called, and then also be able to propagate those up through the call stacks, so I could just query something in the interpreter and get a list back for any random function. It’d be so great, that I can’t even think of a good metaphor for its greatness. That’s how great it would be!
But that’s not where we’re at right now.</RANT>