C++ 如何获取捕获所有异常的消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3132935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:08:21  来源:igfitidea点击:

how to get message of catch-all exception

c++visual-c++exception-handling

提问by 5YrsLaterDBA

If I want to write useful info to a file whenever i caught a catch-all exception, how to do it?

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

try
{
   //call dll from other company
}
catch(...)
{
   //how to write info to file here???????
}

回答by utnapistim

You can't get any information out of the ... catch block. That is why code usually handles exceptions like this:

您无法从 ... catch 块中获取任何信息。这就是为什么代码通常处理这样的异常:

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

回答by Perette

A caught exception is accessible by the function std::current_exception(), which is defined in <exception>. This was introduced in C++11.

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

std::exception_ptr current_exception();

However, std::exception_ptr is an implementation-defined type, so you can't get to the details anyway. typeid(current_exception()).name()tells you exception_ptr, not the contained exception. So about the only thing you can do with it is std::rethrow_exception(). (This functions seems to be there to standardize catch-pass-and-rethrow across threads.)

但是, std::exception_ptr 是实现定义的类型,因此无论如何您都无法了解详细信息。 typeid(current_exception()).name()告诉您 exception_ptr,而不是包含的异常。所以你唯一能做的就是 std::rethrow_exception() 。(这个函数似乎是为了标准化跨线程的 catch-pass-and-rethrow。)

回答by Fred Larson

There's no way to know anything about the specific exception in a catch-all handler. It's best if you can catch on a base class exception, such as std::exception, if at all possible.

在 catch-all 处理程序中没有办法知道任何关于特定异常的信息。如果可能的话,最好能捕获基类异常,例如 std::exception。

回答by sharptooth

You can't get any details. The whole point of catch(...)is to have such "I don't know what can happen, so catch whatever is thrown". You usually place catch(...)after catch'es for known exception types.

您无法获得任何详细信息。重点catch(...)是要有这样的“我不知道会发生什么,所以抓住任何抛出的东西”。你通常发生catch(...)后,catch“ES已知的异常类型。

回答by Marcin

I think he wants to make it log that an error occurred, but doesn't specifically need the exact error (he would write his own error text in that case).

我认为他想让它记录发生了错误,但并不特别需要确切的错误(在这种情况下他会编写自己的错误文本)。

The link DumbCoder posted above has at tutorial that will help you get what you're trying to achieve.

上面发布的链接 DumbCoder 在教程中将帮助您获得您想要实现的目标。