gpt4 book ai didi

c++-cli - 包装 std::exception 并抛出 ApplicationException

转载 作者:行者123 更新时间:2023-12-04 02:58:53 26 4
gpt4 key购买 nike

我在 c++/CLI 库中有以下代码来捕获非托管异常并重新抛出它们:

catch(const std::exception &e)
{
String ^errorMessage = String::Format(L"Parser threw exception: {0}", gcnew String(e.what()));
throw gcnew ApplicationException(errorMessage);
}

这是最好的方法吗?我似乎以这种方式丢失了很多信息。

最佳答案

我假设您的意思是要丢弃派生类型及其包含的任何数据。作为通用解决方案,您无能为力。当然,如果你使用库 Foo,它经常抛出 FooError,你可以 catch (const FooError& e) 并专门处理。

您还可以使用 RTTI 来发现异常的运行时类型并将其添加到 .Net 异常中。弹出来的名字有点丑。

关于c++-cli - 包装 std::exception 并抛出 ApplicationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1935954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com