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

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

问题描述

我正在一个大的非托管C ++库和一个大型C#库中开发一个瘦的托管C ++封装。我需要捕获源自该大型非托管C ++库的错误,并将其重新定义为Clr异常。非托管库抛出以下类的实例:

  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(
格式(文件,行,功能,消息)));
}

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

到目前为止,我已经提出:

 尝试{
//调用一些非托管代码
}
catch(Object *)
{
throw gcnew System :: Exception(发生了什么事情);
}

如何从Error类中提取消息并将其转换为Clr String类,所以我可以将它传递给gcnew System :: Exception()构造函数?
如果非托管代码抛出其他东西,我的catch块是否会捕获它?



编辑:我正在使用catch(Object *),因为这是 MCDN推荐

解决方案

以下内容不适用于您?

  try 
{
//调用一些非托管代码
}
catch(错误const& err)
{
throw gcnew System :: Exception(gcnew System :: String(err.what()));
}

因为这对我有用:

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

struct错误
{
显式错误(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(错误const& err)
{
throw gcnew异常(gcnew String(err.what()));
}
}
catch(异常^ ex)
{
Console :: WriteLine(ex-> ToString());
}
Console :: 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天全站免登陆