Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code reuse in exception handling

I'm developing a C api for some functionality written in C++ and I want to make sure that no exceptions are propagated out of any of the exported C functions.

The simple way to do it is making sure each exported function is contained in a:

try {
   // Do the actual code
} catch (...) {
   return ERROR_UNHANDLED_EXCEPTION;
}

Let's say I know one exception that is often missed inside the C++ code is std::bad_alloc and I want to treat it specially I'd write something like this instead:

try {
   // Run the actual code
} catch (std::bad_alloc& e) {
   return ERROR_BAD_ALLOC;
} catch (...) {
   return ERROR_UNHANDLED_EXCEPTION;
}

Is it possible to decompose this in some clever way so that I can globally treat some errors differently without adding a new catch statement for the exception handler around every exported function?

I'm aware of that this is possible to solve using the preprocessor, but before going down that road, I'd make sure there is no other way to do it.

like image 453
Laserallan Avatar asked May 11 '09 09:05

Laserallan


People also ask

Which block is used to maintain clean up code?

Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. Note: The finally block may not execute if the JVM exits while the try or catch code is being executed.

How do you ensure that your code handles various error scenarios?

You can use the try block to write and execute your program. If no issues arise, it'll pass without any errors. But if any error arises, a catch block is where you can deal with it without shutting down the system.

How do exceptions alter or change program execution?

When an exception occurs in the program, the program execution is terminated. As this is an abrupt termination, the system generates a message and displays it. The message generated by the system may be cryptic like some codes or unreadable.

What is an exception code?

This block of code is called an exception handler. The search begins with the method in which the error occurred and proceeds through the call stack in the reverse order in which the methods were called. When an appropriate handler is found, the runtime system passes the exception to the handler.


2 Answers

You can use only one handler function for all possible exceptions, and call it from each or your API implementation functions, as below:

int HandleException()
{
    try 
    {
        throw;
    }

    // TODO: add more types of exceptions

    catch( std::bad_alloc & ) 
    {
       return ERROR_BAD_ALLOC;
    }
    catch( ... )
    {
        return ERROR_UNHANDLED_EXCEPTION;
    }
}

And in each exported function:

try
{
    ...
}
catch( ... )
{
    return HandleException();
}
like image 170
Jem Avatar answered Oct 02 '22 12:10

Jem


There already is a good answer. But just FYI, its called 'exception-dispatcher' idiom, see C++ FAQ.

like image 40
Abhay Avatar answered Oct 02 '22 11:10

Abhay