如果我想在捕获所有异常时向文件中写入有用的信息,该如何做?
try
{
//call dll from other company
}
catch(...)
{
//how to write info to file here???????
}发布于 2010-06-28 22:01:03
你不能从这里得到任何信息...捕捉块。这就是为什么代码通常会像这样处理异常:
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;
}发布于 2015-08-26 02:42:32
捕获的异常可通过函数std::current_exception()访问,该函数在中定义。这是在C++11中引入的。
std::exception_ptr current_exception();但是,std::exception_ptr是一种实现定义的类型,因此您无论如何都无法获得细节。typeid(current_exception()).name()告诉您exception_ptr,而不是包含的异常。所以你唯一能用它做的就是std::rethrow_exception()。(这个函数似乎是用来标准化线程间的捕获、传递和重新抛出的。)
发布于 2010-06-28 21:55:14
在一个通用的处理程序中,无法知道任何关于特定异常的信息。最好能捕获基类异常,如std::exception,如果可能的话。
https://stackoverflow.com/questions/3132935
复制相似问题