首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取通用异常消息

如何获取通用异常消息
EN

Stack Overflow用户
提问于 2010-06-28 21:53:08
回答 5查看 55.5K关注 0票数 46

如果我想在捕获所有异常时向文件中写入有用的信息,该如何做?

代码语言:javascript
复制
try
{
   //call dll from other company
}
catch(...)
{
   //how to write info to file here???????
}
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-06-28 22:01:03

你不能从这里得到任何信息...捕捉块。这就是为什么代码通常会像这样处理异常:

代码语言:javascript
复制
try
{
    // do stuff that may throw or fail
}
catch(const std::runtime_error& re)
{
    // speciffic handling for runtime_error
    std::cerr << "Runtime error: " << re.what() << std::endl;
}
catch(const std::exception& ex)
{
    // speciffic handling for all exceptions extending std::exception, except
    // std::runtime_error which is handled explicitly
    std::cerr << "Error occurred: " << ex.what() << std::endl;
}
catch(...)
{
    // catch any other errors (that we have no information about)
    std::cerr << "Unknown failure occurred. Possible memory corruption" << std::endl;
}
票数 75
EN

Stack Overflow用户

发布于 2015-08-26 02:42:32

捕获的异常可通过函数std::current_exception()访问,该函数在中定义。这是在C++11中引入的。

代码语言:javascript
复制
std::exception_ptr current_exception();

但是,std::exception_ptr是一种实现定义的类型,因此您无论如何都无法获得细节。typeid(current_exception()).name()告诉您exception_ptr,而不是包含的异常。所以你唯一能用它做的就是std::rethrow_exception()。(这个函数似乎是用来标准化线程间的捕获、传递和重新抛出的。)

票数 16
EN

Stack Overflow用户

发布于 2010-06-28 21:55:14

在一个通用的处理程序中,无法知道任何关于特定异常的信息。最好能捕获基类异常,如std::exception,如果可能的话。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3132935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档