Risking the exception anti-pattern.. with some modifications

Posted by Sridhar Iyer on Stack Overflow See other posts from Stack Overflow or by Sridhar Iyer
Published on 2010-05-20T02:42:30Z Indexed on 2010/05/20 2:50 UTC
Read the original article Hit count: 271

Filed under:
|
|

Lets say that I have a library which runs 24x7 on certain machines. Even if the code is rock solid, a hardware fault can sooner or later trigger an exception. I would like to have some sort of failsafe in position for events like this. One approach would be to write wrapper functions that encapsulate each api a:

returnCode=DEFAULT;
try
{
  returnCode=libraryAPI1();
 }
catch(...)
{
 returnCode=BAD;
}
return returnCode;

The caller of the library then restarts the whole thread, reinitializes the module if the returnCode is bad.

Things CAN go horribly wrong. E.g.

if the try block(or libraryAPI1()) had:

 func1();
 char *x=malloc(1000);
 func2();

if func2() throws an exception, x will never be freed. On a similar vein, file corruption is a possible outcome.

Could you please tell me what other things can possibly go wrong in this scenario?

© Stack Overflow or respective owner

Related posts about anti-patterns

Related posts about c++