异常是否可以自动提供有关其环境的详细信息
Can exceptions automatically provide details about their environment?
我想做什么:
struct A
{
void f()
{
throw auto_exception("something went wrong");
}
}
class Foo
{
void bar()
{
throw auto_exception("step 2 failed");
}
}
异常的what()
字符串应分别读取:
"Exception in A::f(). something went wrong"
"Exception in Foo::bar(). step 2 failed"
这可能吗?
假设您的C++编译器支持__FUNCTION__
(或C++11中的__func__
),您可以将AUTO_EXCEPTION定义为:
#define AUTO_EXCEPTION( msg ) std::runtime_error( std::string(__FUNCTION__) + ": " + msg )
我真的不会把你想要的东西称为"关于环境的信息"。你可以把它与__FILE__
宏和__LINE__
宏以及__func__
(或者__PRETTY_FUNCTION__
在GCC中,或者查看编译器手册以获取其他合适的扩展)拼凑在一起。
可能更有趣的是动态环境,例如引发异常的点的执行跟踪。你可以在运行时使用像libunwind这样的库生成它们。生成跟踪的成本相当高,但由于您仅在发生异常时才执行此操作,因此应该没问题。
相关文章:
- std::extent 实现详细信息说明
- VisualC++ 2010 有没有办法找出有关未处理异常错误的更多详细信息
- 如何使用软化工具包从 OPC UA 服务器异步读取操作回调中的数据值响应中获取 NodeId 详细信息
- protobuf,如何在protobuf消息中遍历所有集合字段,我不知道详细信息?(C++)
- AVX2收集指令使用详细信息
- 只知道运行时的数据类型.如何将数据详细信息隐藏到使用它们的其他类
- CppUnit:如何立即打印故障详细信息
- 我可以为 C++ 类提供不完整的标头以隐藏实现详细信息吗?
- CPP 异常获取抛出调用方的详细信息
- 内存映射 IO 概念详细信息
- 如何在文本文件中搜索员工记录(按名称)并仅显示其详细信息?
- 使用TDD时隐藏文件访问实现详细信息
- 急需有关_doserrno(MSVC)的详细信息
- 主 - Embarcadero C++生成器 10.2 中的详细信息数据集
- 如果构造函数从调用到 std::make_shared 崩溃,GDB 是否可以显示崩溃的详细信息
- 实现 std::tuple 的详细信息
- 如何使 clang 格式将所有详细信息命名空间缩进一个
- 如何从 VisualStudio 获取更多详细信息以跟踪警告的来源
- 增加网格详细信息(附加的镶嵌)
- 异常是否可以自动提供有关其环境的详细信息