Fir*_*cer 24 c++ exception-handling exception
有没有办法捕获原本未处理的异常(包括那些在catch块之外抛出的异常)?
我并不是真的关心所有正常的清理工作,只是因为我可以捕获它,将其写入日志/通知用户并退出程序,因为这些情况下的例外通常是致命的,不可恢复的错误.
就像是:
global_catch()
{
MessageBox(NULL,L"Fatal Error", L"A fatal error has occured. Sorry for any inconvience", MB_ICONERROR);
exit(-1);
}
global_catch(Exception *except)
{
MessageBox(NULL,L"Fatal Error", except->ToString(), MB_ICONERROR);
exit(-1);
}
Run Code Online (Sandbox Code Playgroud)
Evi*_*ach 26
这可用于捕获意外的异常.
catch (...)
{
std::cout << "OMG! an unexpected exception has been caught" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
没有try catch块,我认为你不能捕获异常,所以构造你的程序,所以异常的代码是在try/catch的控制下.
kra*_*lyk 20
gbj*_*anb 11
您可以在Windows上使用SetUnhandledExceptionFilter,它将捕获所有未处理的SEH异常.
通常,这对于您的所有问题都已足够,因为IIRC将所有C++异常实现为SEH.
小智 8
没有任何catch块,您将不会遇到任何异常.你可以在main()中使用catch(...)块(以及每个附加线程中的等效块).在此catch块中,您可以恢复异常详细信息,并且可以对它们执行某些操作,例如记录和退出.
但是,一般的catch(...)块也存在缺点:系统发现异常已由您处理,因此它不再提供任何帮助.在Unix/Linux上,此帮助将构成一个CORE文件,您可以将其加载到调试器中并查看未触发异常的原始位置.如果你用catch(...)处理它,这些信息就会丢失.
在Windows上,没有CORE文件,所以我建议使用catch(...)块.从该块开始,您通常会调用一个函数来恢复实际的异常:
std::string ResurrectException()
try {
throw;
} catch (const std::exception& e) {
return e.what();
} catch (your_custom_exception_type& e) {
return e.ToString();
} catch(...) {
return "Ünknown exception!";
}
}
int main() {
try {
// your code here
} catch(...) {
std::string message = ResurrectException();
std::cerr << "Fatal exception: " << message << "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
更新:这仅涵盖c ++ 98.
从Meyers的更有效的C++(第76页)中,您可以定义一个函数,当函数生成未由其异常规范定义的异常时,该函数被调用.
void convertUnexpected()
{
// You could redefine the exception here into a known exception
// throw UnexpectedException();
// ... or I suppose you could log an error and exit.
}
Run Code Online (Sandbox Code Playgroud)
在您的应用程序中注册功能:
std::set_unexpected( convertUnexpected );
Run Code Online (Sandbox Code Playgroud)
如果函数生成一个未由其异常规范定义的异常,则会调用您的函数convertUnexpected()...这意味着只有在使用异常规范时这才有效.(
这就是我在 main() 中经常做的
int main()
{
try
{
// Do Work
}
catch(std::exception const& e)
{
Log(e.what());
// If you are feeling mad (not in main) you could rethrow!
}
catch(...)
{
Log("UNKNOWN EXCEPTION");
// If you are feeling mad (not in main) you could rethrow!
}
}
Run Code Online (Sandbox Code Playgroud)
如果 C++11 可用,则可以使用此方法(参见示例:http : //en.cppreference.com/w/cpp/error/rethrow_exception):
#include <iostream>
#include <exception>
void onterminate() {
try {
auto unknown = std::current_exception();
if (unknown) {
std::rethrow_exception(unknown);
} else {
std::cerr << "normal termination" << std::endl;
}
} catch (const std::exception& e) { // for proper `std::` exceptions
std::cerr << "unexpected exception: " << e.what() << std::endl;
} catch (...) { // last resort for things like `throw 1;`
std::cerr << "unknown exception" << std::endl;
}
}
int main () {
std::set_terminate(onterminate); // set custom terminate handler
// code which may throw...
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这种方法还允许您为未处理的异常自定义控制台输出:有这样的东西
unexpected exception: wrong input parameters
Aborted
Run Code Online (Sandbox Code Playgroud)
而不是这个:
terminate called after throwing an instance of 'std::logic_error'
what(): wrong input parameters
Aborted
Run Code Online (Sandbox Code Playgroud)