My preferred scripting language at this time is Python. If no other considerations rule it out, I will choose Python, for its succinctness, its tightness, and for portability.
All languages have their flaws and peccadillos. Most languages have associated libraries that were thought out much less well than the languages themselves. But that isn’t really the language’s fault. Here, I’ll concentrate on a scenario that has repeated several times in my experience programming in Python. It is a combination of: a flawed design for exception handling and spotty documentation.
Want to ensure a directory exists.
Use makedirs
Find that, if path already exists, it throws OSError
.
But, exactly where in the docs do we find this?
makedirs
OSError
is an EnvironmentError
, and to look there for valuesEnvironmentError
, possible values for OSError
are not listed
What other conditions might result in OSError
being thrown
by makedirs
?
I still don’t know.
What if I wanted the program to behave differently depending on the kind
of OSError
?
Well, farting about more with the docs, we find in the top-level
os
module doc
python2.4/html/lib/module-os.html
that we should look in module errno. There we kind of find
out how to distinguish OSError
values (still plenty of
guesswork ahead).
I found out about this OSError
by running the program and
seeing how it failed.
Generally, how do you know a Python function throws a given exception
before it throws it?
Then there’s the question of how many arguments to catch for a given
exception. An except
statement that lists the wrong
arguments for the exception it catches can look OK, compile, and
crash the program when it executes.
Much better would be enforced exception handling. At least, that some function in the tree handles each exception.
Much better would be exception arguments checking
in except
statements.
Much better would be an auto documentation feature that lists all exceptions thrown by each function.