Click here to Skip to main content
15,891,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C++
void Metrics::GetNumbers(InputParams* input, OutputParams* output)
{
    int i, x, k;
    int status = 0;
    char* mesg;
    ............;
    ............;

    try{
         const char* name = input->getName(&status, &mesg); //this returns a name string 
         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&)
     {
         // TODO
     }
     catch (...)
     {
         // TODO
     }


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,
C++
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,

C++
const char* name = input->getName(&status, &mesg);
CheckException(name, status);  //this function should return an exception if either name or status fails.

const char* age = input->getAge(&status, &mesg);
CheckException(age, status); //this function should return an exception if either age or status fails.
Am not sure how to design something like that. I would appreciate if someone can give me some ideas.
Posted
Updated 8-Feb-19 23:32pm
v2

1 solution

You could modify your DLL code so it rethrows the exception after posting the details. That way the client does not need to check anything, unless it wants to catch the exception and take some action.
 
Share this answer
 
Comments
Donguy1976 9-Feb-19 8:55am    
Client doesn't need to do anything here. The exception needs to be handled just within the DLL code. The IF condition I wrote above is just to catch the exception within DLL and process. i.e., write the exception info to a log file or something.

But I still don't get, how to move that IF condition block to a separate function, so that it doesn't need to be duplicated multiple times in the GetNumbers function.
Richard MacCutchan 9-Feb-19 12:38pm    
Like I said, rethrow the exception in the DLL, and the client does not need to do anything.
Donguy1976 9-Feb-19 19:47pm    
Can you please provide some sample code on rethrowing the exception?
Richard MacCutchan 10-Feb-19 4:46am    
Just throw the object that is passed in to the catch block.

You could also go and read the documentation on exception handling.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900