如何捕捉非托管C ++异常在托管C ++ [英] How to catch unmanaged C++ exception in managed C++

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

问题描述

我开发了一个大型的非托管C ++库,以及一个大型的C#库薄托管C ++包装。我需要赶上原产于大型非托管C ++库的错误,并重新抛出它们作为CLR异常。非托管库抛出下面的类的实例:

 错误::错误(常量的std :: string的&安培;文件,长,株, 
常量为std :: string&放大器;函数,
常量标准::字符串&安培;消息){
消息_ =提高:: shared_ptr的<标准::字符串>(新标准::字符串(
格式(文件,行,功能,消息)));
}

为const char *错误::什么()const的掷(){
返回消息_-> c_str();
}



到目前为止,我想出了这一点:

  {试
//调用一些非托管代码
}
赶上(对象*)
{
掷gcnew系统:异常(坏的东西发生了);
}



我如何从错误类别中提取邮件,并将其转换为CLR字符串类,这样我可以将它传递给gcnew系统:异常()构造函数?
。如果非托管代码抛出别的东西,我的catch块捕获它



编辑:我使用捕捉(对象*),因为这是的在MCDN 推荐


解决方案

执行以下操作对你不行?



<预类=郎-CPP prettyprint-覆盖>
{
//调用一些非托管代码
}
赶上(错误常量和放大器; ERR)
{
罚球gcnew系统:异常( gcnew系统::字符串(err.what()));
}

由于这肯定对我的作品:



<预类=郎-CPP prettyprint-覆盖> 的#pragma管理(推,关)
&#包括LT;字符串>

结构误差
{
明确的错误(标准::字符串常量和放大器;消息):消息_(消息){}
字符常量*什么()const的掷( ){返回message_.c_str(); }

私人:
标准::字符串消息_;
};

无效SomeFunc()
{
抛出错误(信息放在这里);
}

的#pragma管理(POP)

INT main()中使用的命名空间系统
{
;


{

{
SomeFunc();
}
赶上(错误常量和放大器; ERR)
{
掷gcnew异常(gcnew字符串(err.what()));
}
}
赶上(例外^前)
{
控制台:的WriteLine(EX->的ToString());
}
控制台:的ReadLine();
}


I am developing a thin managed C++ wrapper over a large unmanaged C++ library, and a large C# library. I need to catch errors originating in that large unmanaged C++ library, and rethrow them as Clr exceptions. The unmanaged library throws instances of the following class:

Error::Error(const std::string& file, long line,
             const std::string& function,
             const std::string& message) {
    message_ = boost::shared_ptr<std::string>(new std::string(
                                  format(file, line, function, message)));
}

const char* Error::what() const throw () {
    return message_->c_str();
}

So far I have come up with this:

try{
// invoke some unmanaged code
}
catch(Object*)
{
throw gcnew System::Exception("something bad happened");
}

How do I extract the message from Error class and convert it to the Clr String class, so that I can pass it to gcnew System::Exception() constructor? If the unmanaged code throws something else, will my catch block catch it?

Edit: I am using catch(Object*) because that is recommended in MCDN

解决方案

Does the following not work for you?

try
{
    // invoke some unmanaged code
}
catch (Error const& err)
{
    throw gcnew System::Exception(gcnew System::String(err.what()));
}

Because this certainly works for me:

#pragma managed(push, off)
#include <string>

struct Error
{
    explicit Error(std::string const& message) : message_(message) { }
    char const* what() const throw() { return message_.c_str(); }

private:
    std::string message_;
};

void SomeFunc()
{
    throw Error("message goes here");
}

#pragma managed(pop)

int main()
{
    using namespace System;

    try
    {
        try
        {
            SomeFunc();
        }
        catch (Error const& err)
        {
            throw gcnew Exception(gcnew String(err.what()));
        }
    }
    catch (Exception^ ex)
    {
        Console::WriteLine(ex->ToString());
    }
    Console::ReadLine();
}

这篇关于如何捕捉非托管C ++异常在托管C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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