apifunc() and apifunc2() translate exceptions into error codes using the dispatcher() function.
Basically, the following happens: apifunc() (and similarly apifunc2() ) tries to call the foo() function. If foo() throws an exception, then the catch calls dispatcher() to get the error code corresponding to the exception, and then returns this error code. If foo() does not throw, apifunc() returns an ErrorCode_Fine indicating no error.
dispatcher() works by throwing the last exception thrown, i.e. throws one foo() . dispatcher() then checks which exception was thrown using catch blocks and returns the correct error code. For example, if foo() chose std::bad_alloc , then this catch block will be executed and return ErrorCode_OutOfMemory; .
Why would anyone do this?
Exceptions are not necessarily binary compatible in different compilations (compilers, compiler flags, etc.), so the translation of exceptions from error codes is more portable across module boundaries.
source share