当前位置: 首页 > ai >正文

(C++17) 未捕获异常 uncaught_exceptions

文章目录

    • 🐾前言
    • 🐾实例
      • 🪝工具类
      • 🪝测试
    • ⭐END
      • 🌟交流方式

🐾前言

从 C++11 其有 std::uncaught_exception 用于判断当前线程是否有未捕获的异常。而这个函数只是返回 bool 仅能查看是否存在未捕获异常。

因此到了 C++17 被弃用,在 C++20 中移除。C++17 引入了 int std::uncaught_exceptions() 可以获得具体的数量。

std::uncaught_exception, std::uncaught_exceptions - cppreference.com

🐾实例

🪝工具类

其实这个非常简单,最常用的就是在析构函数的时候,进行获取判断。

因此可以写这么一个工具类。当然,是选择在判断出有异常还是没异常的时候触发这个 action 还是两种状态下都注册一个,都是可以的。

#include <exception>
#include <functional>class ExcCatch {
private:const int             count = 0;std::function<void()> action;public:ExcCatch(const std::function<void()>& action = {}): count(std::uncaught_exceptions()), action(action) {}~ExcCatch() {if (count != std::uncaught_exceptions()) {if (action) {action();}}}
};

🪝测试

此处是用 vscode 中的 Copilot 所生成的测试代码。

简单看看使用方式就行了。

#include <exception>
#include <functional>class ExcCatch {
private:const int             count = 0;std::function<void()> action;public:ExcCatch(const std::function<void()>& action = {}): count(std::uncaught_exceptions()), action(action) {}~ExcCatch() {if (count != std::uncaught_exceptions()) {if (action) {action();}}}
};#include <atomic>
#include <iostream>
#include <stdexcept>// Helper for test output
#define ASSERT_TRUE(cond, msg)                      \if (!(cond)) {                                  \std::cout << "[FAIL] " << msg << std::endl; \} else {                                        \std::cout << "[PASS] " << msg << std::endl; \}
#define ASSERT_FALSE(cond, msg) ASSERT_TRUE(!(cond), msg)// Test that ExpCatch does not call action if no exception is thrown
void test_NoException_ActionNotCalled() {std::atomic<bool> called{false};{ExcCatch guard([&] { called = true; });}ASSERT_FALSE(called, "NoException_ActionNotCalled");
}// Test that ExpCatch calls action if exception is thrown
void test_Exception_ActionCalled() {std::atomic<bool> called{false};try {ExcCatch guard([&] { called = true; });throw std::runtime_error("test");} catch (...) {// Exception is caught here}ASSERT_TRUE(called, "Exception_ActionCalled");
}// Test that ExpCatch works with empty action
void test_EmptyAction_NoCrash() {try {ExcCatch guard;throw std::runtime_error("test");} catch (...) {// Should not crash}std::cout << "[PASS] EmptyAction_NoCrash" << std::endl;
}// Test that ExpCatch does not call action if exception is thrown but not caught in scope
void test_ExceptionNotCaughtInScope_ActionCalled() {std::atomic<bool> called{false};auto              test_func = [&] {ExcCatch guard([&] { called = true; });throw std::runtime_error("test");};try {test_func();} catch (...) {// Exception is caught here}ASSERT_TRUE(called, "ExceptionNotCaughtInScope_ActionCalled");
}int main() {test_NoException_ActionNotCalled();test_Exception_ActionCalled();test_EmptyAction_NoCrash();test_ExceptionNotCaughtInScope_ActionCalled();return 0;
}

⭐END

🌟交流方式

关注我,学习更多C/C++,python,算法,软件工程,计算机知识!

⭐交流方式⭐ |C/C++|算法|设计模式|软件架构-CSDN社区

B站

👨‍💻主页:天赐细莲 bilibili

http://www.xdnf.cn/news/9088.html

相关文章:

  • Java基础 Day21
  • 从无符号长整型数中提取字节
  • 【Redis】Redis安装
  • 红外遥控器接收实验:CubeMX配置底层软件
  • 基于vue框架的动漫网站noww0(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 【windwos】文本编辑器Notepad++ 替代品Notepad--
  • 汇川伺服软件设置提示使能冲突
  • 深入解读Qwen3技术报告(五):后训练对齐
  • Linux系统调用深度剖析
  • 佳易王商品进出库管理系统:数字化库存管理的全能解决方案#海鲜蔬果批发管理#批发出库管理
  • 双臂机器人运动空间与干涉分析仿真技术报告
  • 功能“递归模式”在 C# 7.3 中不可用,请使用 8.0 或更高的语言版本的一种兼容处理方案
  • 【产品经理】如何撰写产品文档
  • 解锁webpack:处理跨域devserver、摇树treeshaking、图片压缩sharp
  • ACM模式中输入输出的处理
  • 硅基计划2.0 学习总结 肆 初稿
  • 深度学习---可视化
  • Linux wget 常用命令详解
  • AI时代新词-AI伦理(AI Ethics)
  • 【Linux】磁盘管理,虚拟机CentOS扩展根分区
  • 像造汽车一样造房子:装配式建筑4.0如何重塑未来人居
  • 启程:MCP开发环境配置和旅游攻略案例体验
  • 基于NSGA2算法的无人机航迹规划算法
  • BaseProviderMultiAdapter多布局总结
  • vertica优化
  • 网口XDP-报文回环转发
  • 知识图谱系列(3):构建方法与流程
  • vue3获取两个日期之间的所有时间
  • 解决leetcode第3509题.最大化交错和为K的子序列乘积
  • OceanBase数据库全面解析(高级特性篇)