This is more of a design suggestion that I want to hear from C++ experts here.
I have a C++ DLL in VS 2015 IDE. I want to add more exception handling into this code.
Currently there's a function as follows.
void Metrics::GetNumbers(InputParams* input, OutputParams* output)
{
int i, x, k;
int status = 0;
char* mesg;
............;
............;
try{
const char* name = input->getName(&status, &mesg); and status will contain whether success or failure and mesg will have any error
messages.
const char* age = input->getAge(&status, &mesg);
..............................................;
..............................................;
vector <int> myVectorNumers = input->getNumberArray(&status, &mesg);
vector <int> myVectorInfos = input->getInfoArray(&status, &mesg);
...............................................................;
}
catch (std::exception e&)
{
}
catch (...)
{
}
The above code is in DLL. And
input is a pointer that points to the client that calls this DLL. So the DLL queries the client for certain info as above.
It gives the info as a return to those calls and also the status and mesg will also contain info regarding whether those calls were successful.
None of the client (getName, getAge,...) functions return an exception and i need to handle that myself.
I could handle the exception after every call to client.
What I have tried:
Something like this,
const char* age = input->getAge(&status, &mesg);
std::exception ex;
if (age == NULL || status == ERROR)
{
throw ex;
}
I have to add above IF condition after every
input call. Instead of that, ideally it should be another function that will check whether pointer is NULL and status is ERROR and then return an exception.
So basically what i mean is a more better code rather than duplicating the same lines multiple lines within the same function.
In ideal scenario, the code should be like this,
const char* name = input->getName(&status, &mesg);
CheckException(name, status);
const char* age = input->getAge(&status, &mesg);
CheckException(age, status);
Am not sure how to design something like that. I would appreciate if someone can give me some ideas.