Exceptions in the Emulator Lib

Topics: Emulator Library
Dec 5, 2010 at 12:07 AM
Edited Dec 5, 2010 at 12:07 AM

You asked in your code if you should throw exceptions in two different spots to deal with bad input from parameters in SetFountainHardware or ParseToFountainState. According to these design guidelines for frameworks:

"Do report execution failures by throwing exceptions. If a member cannot successfully do what is designed to do, it should be considered an execution failure and an exception should be thrown."

The article is a good read, and I think you should definitely throw exceptions for these methods. You should add a packet validator so that the programmer will know if it should try to parse the packet that an exception will be raised.

public bool PacketIsValid(byte[] message)
     // check to see if it is the right size.
     if(message.Length != possible sizes)
          return false;

     // check to see if it has the correct switches
     if (!hasCorrectSwitches(message))
          return false;

     return true;

The exceptions that you throw should also be a part of the standard System.Exception namespace. I would recommend using ArgumentException for SetFountainHardware and ParseToFountainState. Make sure to fill in the exception messages so the programmer knows what they did wrong.