K
Kenneth Porter
I've read this article and have some followup questions.
http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thr
ead/9d5324ce02f4d89b/
I'm working on an embedded robotics application that cannot terminate.
I often have the need to temporarily change some setting before
performing an operation. If the operation fails, I still need to restore
the original setting. It's possible for the restore operation to fail
(eg. a communication fault that temporarily prevents the request from
reaching the hardware), in which case user intervention is required. The
application can later re-initialize the system to a known state.
A typical scenario, written in C (assuming a zero errorcode means no
error):
int oldvalue;
int errorcode = SaveOldValue(&oldvalue);
if (!errorcode)
errorcode = SetValue(newvalue);
if (!errorcode)
errcode = PerformOperation();
int errcode2 = SetValue(oldvalue);
return errcode ? errcode : errcode2;
This idiom might be nested.
With C++, it seems like I should report errors via exceptions, and
perform the temporary settings changes with RAII objects (to insure that
original settings are restored through all error paths).
How can I capture the error if the restore operation fails? Do I simply
use uncaught_exception() in the RAII dtor to throw only if I'm not
handling an exception from the main operation?
http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thr
ead/9d5324ce02f4d89b/
I'm working on an embedded robotics application that cannot terminate.
I often have the need to temporarily change some setting before
performing an operation. If the operation fails, I still need to restore
the original setting. It's possible for the restore operation to fail
(eg. a communication fault that temporarily prevents the request from
reaching the hardware), in which case user intervention is required. The
application can later re-initialize the system to a known state.
A typical scenario, written in C (assuming a zero errorcode means no
error):
int oldvalue;
int errorcode = SaveOldValue(&oldvalue);
if (!errorcode)
errorcode = SetValue(newvalue);
if (!errorcode)
errcode = PerformOperation();
int errcode2 = SetValue(oldvalue);
return errcode ? errcode : errcode2;
This idiom might be nested.
With C++, it seems like I should report errors via exceptions, and
perform the temporary settings changes with RAII objects (to insure that
original settings are restored through all error paths).
How can I capture the error if the restore operation fails? Do I simply
use uncaught_exception() in the RAII dtor to throw only if I'm not
handling an exception from the main operation?