1. Catch something specific
if __name__ == "__main__":try:
run(None)
sys.exit(0)
except ValueError:
sys.exit(1)
So, what does this do if another type of error is thrown? Why is it catching a specific type of error? The rest of the code is logging info - why don't we log the error? How does this make troubleshooting easy, or even possible?
2. Throw something specific
The pattern above is of course fine when the run function works like this:
def run(config):
try:
catch:
raise ValueError
3. Tell no-one
Make the last line in you script
exit(0)
regardless of whatever just happened. There was a chance the script could return the exit code of whatever it called, so typing the extra line is first, effort, second achieves nothing, and finally means no-one will notice if something went wrong. Or at least not at the time. Perhaps whoever did that thought a shell script won't exit without the keyword exit at the end.
If you're here and want some C++, Martin Janzen wrote a great article for ACCU's Overload about lifetime management and std::exit.
Here is the link: https://accu.org/journals/overload/31/177/overload177.pdf#page=12
No comments:
Post a Comment