Monday, February 23, 2009

Handling Errors

The unfortunate side effect of any program is errors and we’ve all had encounters with confusing error messages like “There was an error in variable callSec() F2b2”. Error messages are used to give the programmer information as well as the user. The information that the programmer needs is very different from the message that users want to see.

In my program, MTG Forge, I don’t really have an error handling strategy, like always writing the error to a file. When I am programming MTG Forge shows errors on the command line which my IDE (Word for programmers) displays. Unfortunately when MTG Forge is running as a normal program and an error occurs, nothing is displayed to the user. Obviously this is a major oversight and is a result bad planning.

The best way to handle errors is to have them automatically uploaded into a database on the Internet. This presumes that the program will always be online. Another good strategy is to have the program write the error message to a file, so the program can send the file when it is connected to the Internet or have the user e-mail the file.

In order to fix an error I need to know what the error message is (such as variable cardList is null), what is the line number, and what caused the error (like was the computer trying to use Elvish Piper?). Errors will happen in any computer program and you have to consider how your program will handle them.

4 comments:

  1. You make it sound like you're using Word to program :P

    ReplyDelete
  2. In version 2.0, I'm using a generic global static method, Error.reportError(String) to handle all errors. During development I can throw exceptions and when I deploy it for end users, it can show an error message and write it to a logfile. I use JBuilder.

    ReplyDelete
  3. have you ever tried log4j?
    it's a very good tool really

    the major features are the following
    1. you have several levels of logging: debug, info, warn, error
    as far as I know, it is also possible to add custom levels as well
    and then you just write
    log.error("nullpointerexception for card" + card)
    or
    log.info("player " + player + " has " + count + " cards")
    2. you have separate property file that says to the programs what, when and where to log
    2a. you can say that all classes logs only errors and warns and ClassA logs also info
    2b. you can say where to log: console, file by just adding few lines - you can even separate: errors goes to main log file, infos to more detailed file
    2c. you can change log info
    for our example, instead of writing
    "player MTGForge has 7 cards"
    by adding lines like
    log4j.appender.file.layout.ConversionPattern=%-5p [%d{yyyy-MM-dd HH:mm [ss:SSS]}] %C{1}[%t]: %m%n


    will write
    INFO [2009-02-26 07:46 [43:372]] HandClass[Thread-2]: player MTGForge has 7 cards
    or even
    INFO HandClass->drawCards(line:57) - player MTGForge has 7 cards

    and all this just without changing any code

    ReplyDelete

Note: Only a member of this blog may post a comment.