处理C ++ DLL中的异常 [英] Handling exception in C++ DLL

查看:142
本文介绍了处理C ++ DLL中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更像是我想在这里听到C ++专家的设计建议。



我在VS 2015 IDE中有一个C ++ DLL。我想在此代码中添加更多异常处理。



目前有一个函数如下。

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); //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
     }





上面的代码在DLL中。 输入是一个指向调用此DLL的客户端的指针。因此DLL会向客户端查询上面的某些信息。



它将信息作为这些调用的返回值,状态和mesg也将包含有关是否的信息这些调用是成功的。



没有客户端(getName,getAge,...)函数返回异常,我需要自己处理。



我可以在每次打电话给客户后处理异常。



我尝试过:



这样的东西,



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;
}

我必须在每次输入调用后添加以上IF条件。而不是那样,理想情况下它应该是另一个函数来检查指针是否为NULL并且状态是ERROR然后返回异常。



所以基本上我的意思是更好的代码而不是在同一个函数中复制相同的行多行。



在理想情况下,代码应该是这样的,



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);  //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.

推荐答案

您可以修改您的DLL代码,以便在发布详细信息后重新抛出异常。这样客户端不需要检查任何内容,除非它想要捕获异常并采取一些行动。
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.


这篇关于处理C ++ DLL中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆