On 7/27/2010 8:48 AM, Torsten wrote:
[...]
int write(void* handle, char* command, long timeout)
int read(void* handle, char** response, size_t buffersize, long
timeout)
Both return an error code, since the commands can fail for several
reasons that are not in the scope of the programmer (camera timeout,
camera not connected, ...).
No I want to wrap this library. A member function of the camera shall
send a command to the camera and return it's response, e.g.
std::string Camera::sendCommand(const std::string &command);
My question: what is the best way to handle these errors:
What exactly should happen when the camera is not connected or when a
timeout occurs?
On a timeout, I would like to try to send the command once more, maybe
write a log message, and inform the user. When the camera in not
connected, I would like to inform the usere immediately. Many differnt
approaches are possible. I haven't decides that yet.
For example, if there's just a message to be displayed to the user, then
I don't see how the use of exceptions could possibly lead to all
sendCommand calls being wrapped in try/catch blocks.
The code to send the command for setting gain to a special value a
second time if it was not successfull the first time, might look like:
try {
// send it the first time:
camera.sendCommand("set gain to value xyz");
}
catch(TimoutException)
{
// send it the second time:
camera.sendCommand("set gain to value xyz");
// don't catch the exception, if it fails twice
}
Doesn't look very nice to me.
That's because you're putting the logic in the wrong place...
std::string Camera::sendCommand(const std::string &command, int
maxRetries=1) {
for (int retry = 0;;) {
try {
return doSendCommand(command);
} catch(TimeoutException) {
++retry;
if (retry >= maxRetries) {
// Pardon me if the syntax is a little off,
// I'm more frequently a Java programmer, and that's where I have
// experience with exceptions.
throw; // Rethrow the exception.
}
}
}
}
Not the prettiest, but it is fully encapsulated in one place.
BTW, I would suggest making that a private method, and adding public
methods which wrap up the individual commands.
void Camera::setGain(const Gain &gain) {
sendCommand("set gain to value " + gain.asString());
Yes, that is actually what I do. I just tried to keep my posting short.
...
...
And only the CameraLinkCamera class generates exceptions. :-)
Thanks for your advice anyway. You all have been very helpfull.