假设我有:
try
{
externalLibrary::doSomething();
}
catch (std::exception &e)
{
//yay I know what to do
}
catch (...)
{
//darn, I've no idea what happened!
}
在某些情况下,您可能会遇到异常,但您不知道它来自哪里或为什么 - 在某些没有调试信息的外部库中。有没有办法找到抛出的内容,或者以其他方式获取与之相关的任何数据?他们might正在做:
throw myStupidCustomString("here is some really useful information");
但我永远不知道我是否能抓住...
如果有必要的话,可以使用 MSVC++ 2008。
如果您使用 gcc 或 CLANG,您可以使用一个技巧来了解“未知”异常类型。请记住,这是非标准的!
#include <cstdlib>
#include <iostream>
#include <cxxabi.h>
using namespace __cxxabiv1;
std::string util_demangle(std::string to_demangle)
{
int status = 0;
char * buff = __cxxabiv1::__cxa_demangle(to_demangle.c_str(), NULL, NULL, &status);
std::string demangled = buff;
std::free(buff);
return demangled;
}
struct MyCustomClass
{};
int main(int argc, char * argv[])
{
try
{
throw MyCustomClass();
}
catch(...)
{
std::cout << "\nUnknown exception type: '" << util_demangle(__cxa_current_exception_type()->name()) << "'" << std::endl;
}
return(0);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)