I have a control circuit that I communicate with via serial port. If the response command doesn't match a certain format I consider it to be an error and was wondering if I should return an error code or throw an exception? For example:
public double GetSensorValue(int sensorNumber)
{
...
string circuitCommand = "GSV,01," + sensorNumber.ToString(); // Get measurement command for specified sensor.
string responseCommand;
string expectedResponseCommand = "GSV,01,1,OK";
string errorResponseCommand = "ER,GSV,01,1,62";
responseCommand = SendReceive(circuitCommand); // Send command to circuit and get response.
if(responseCommand != expectedResponseCommand) // Some type of error...
{
if(responseCommand == errorResponseCommand) // The circuit reported an error...
{
... // How should I handle this? Return an error code (e.g. -99999) or thrown an exception?
}
else // Some unknown error occurred...
{
... // Same question as above "if".
}
}
else // Everything is OK, proceed as normal.
...
}
Thanks!
Error code.
When it is a good practice to throw exceptions:
In your case throwing an exception is better. Because the caller of the method can differentiate a "legitimate" output and an error condition. If you return an error code it is still a numeric value so the caller may misinterpret it as an output from control circuit.
Personally, I like to deal with this situation by defining a response object of some kind. I've arrived at this via experience doing this and weighing what should be considered exceptional. If you're interfacing with some device and the user turns the device off or something, this is an exceptional condition, and you'd throw an exception.
If the particular method call to the device fails or something, that's not really exceptional, but it is an error. I don't want to define a magic number if I'm returning a double, and I don't want to throw an exception. So, I define something like:
The specifics here aren't really important -- you'll adapt it to what clients of your method want. In this example, the client API would be to check for valid and, if so, use the return value -- no magic number and no exception. The paradigm is what is more important. The way I see it, you're using an object oriented language -- so you might as well use objects. :)
I would recommend using exceptions in most cases due to a few simple facts:
There are a few things to consider though:
But as a general rule, I'd say: stick to Exceptions, it's simple, informative, debuggable and easy to handle even though it might come at a slight performance cost
Strictly speaking an exception would be used when you're in an 'exceptional' circumstance, i.e. one that you wouldn't expect.
If it were me I'd probably return an error code in the first case (known error response), and throw an exception in the second case (unknown error code)
In almost all cases I would communicate errors via exceptions. I would almost never use "error codes" as such - occasionally it's useful to give success/failure along with a result as in
int.TryParse
etc, but I don't think this situation works like that. This sounds like a real error condition which should stop further progress though, so an exception is appropriate.EDIT: As noted in comments, if the circuit reporting an error really is "expected" and the caller should be able to deal with it and should actively be looking for that situation, then it's reasonable to use a status code.
Unfortunately error handling is one of those things that we haven't really got right in software engineering yet...