The proper response to lousy error message

error messages

I've just come across (for the 47 millionth time) some code that looks like this:

except IOError, e:
  print "Problems reading file: %s." % filename
  sys.exit( 1 )

My first reaction is very visceral: the person who coded this is a complete idiot. How hard is it to print error messages to stderr and to include the system error message in the string? It certainly used to be the case that, for simple programs (command line filters), best practice was to print error messages to stderr and (when appropriate) to include the system error in the message. A very large percentage of developers no longer follow those rules. Many questions of the form "what is best practice for error messages" include answers about massive logging frameworks and exception handling mechanisms that do not apply to the simple filter. Has best practice changed? or is failure to include system error messages and printing error messages to the wrong stream the mark of a novice?

Best Answer

Either fix it yourself or report a bug to the programmer responsible for this to fix it.

Additionally there are two code smells in this:

  • No logging framework.
  • Code should not just abruptly exit.

It is important to have the exception bubble to the main method, have it logged properly and THEN exit. This allows upstream code to properly wrap things up - close files, empty buffers, release system resources, etc.

Related Topic