Instead of wrapping each and every onEntry, onExit
and action
in try catch block, better approach would be to implement noexcept wrapper versions of onEntry, onExit and action that would simply call onEntry, onExit
and action
within try catch block and handle any exceptions thrown.
In code you simply call only wrappers instead of actual functions, using exception handling to your advantage but avoiding duplicating try/catch logic.
void onExitWrapper(int arg1, int arg2) noexcept
{
try
{
onExit(arg1,arg2);
}
catch(/*whatever*/)
{
//handle exception
}
}
If exception handling is similar for different functions, you can also create special function that receives function pointer/lambda and input arguments, and calls function referenced by function pointer with input arguments inside try catch:
template <class Arg1,class Arg2,class Ret> Ret exceptionHandler(Ret (funcPtr *)(Arg1,Arg2),Arg1 arg1,Arg2 arg2)
{
Ret output;
try
{
output = funcPtr(arg1,arg2);
}
catch(/*whatever*/)
{
//handle exceptions
}
return output;
}