C++ Exception

2023-05-16

Exception type:

Derived types (scattered throughout different library headers)


Indirectly (through  logic_error ):

Indirectly (through  runtime_error ):

Multiple handlers (i.e., catch expressions) can be chained; each one with a different parameter type. Only the handler whose argument type matches the type of the exception specified in the throw statement is executed.

If an ellipsis (...) is used as the parameter of catch, that handler will catch any exception no matter what the type of the exception thrown. This can be used as a default handler that catches all exceptions not caught by other handlers:

1
2
3
4
5
6
try {
  // code here
}
catch (int param) { cout << "int exception"; }
catch (char param) { cout << "char exception"; }
catch (...) { cout << "default exception"; }
 


In this case, the last handler would catch any exception thrown of a type that is neither  int  nor  char .

After an exception has been handled the program, execution resumes after the  try-catch  block, not after the  throw statement!.

It is also possible to nest try-catch blocks within more external try blocks. In these cases, we have the possibility that an internal catch block forwards the exception to its external level. This is done with the expression throw; with no arguments. For example: 

1
2
3
4
5
6
7
8
9
10
11
try {
  try {
      // code here
  }
  catch (int n) {
      throw;
  }
}
catch (...) {
  cout << "Exception occurred";
}

Exception specification

Older code may contain  dynamic exception specifications. They are now deprecated in C++, but still supported. A  dynamic exception specification follows the declaration of a function, appending a  throw specifier to it. For example:

 
double myfunction (char param) throw (int);
 


This declares a function called  myfunction, which takes one argument of type  char and returns a value of type  double. If this function throws an exception of some type other than  int, the function calls  std::unexpected instead of looking for a handler or calling  std::terminate.

If this  throw specifier is left empty with no type, this means that  std::unexpected is called for any exception. Functions with no  throw specifier (regular functions) never call  std::unexpected, but follow the normal path of looking for their exception handler.

1
2
int myfunction (int param) throw(); // all exceptions call unexpected
int myfunction (int param);         // normal exception handling 


void unexpected();
  
Function handling unexpected exceptions
Calls the current  unexpected handler.

By default, the  unexpected handler calls  terminate. But this behavior can be redefined by calling  set_unexpected.

This function is automatically called when a function throws an exception that is not listed in its  dynamic-exception-specifier (i.e., in its  throw specifier).

This function is provided so that the  unexpected handler can be explicitly called by a program, and works even if set_unexpected has not been used to set a custom  unexpected handler (calling  terminate in this case).


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++ Exception 的相关文章

随机推荐

  • 软件工程——结构化分析方法

    结构化方法 概念 用来指导软件项目的开发 一种系统化的软件开发方法包括 xff1a 结构化分析方法 结构化设计方法 结构化程序设计方法 结构化设计方法和结构化程序设计方法的区别 xff0c 前者指的软件开发设计阶段的软件体系架构以及内部模块
  • linux安装软件方式--源码编译安装

    简介 xff1a 介绍源码编译安装软件包的管理 1 源码安装优点 xff1a 编译安装过程 xff0c 可以设定参数 xff0c 指定安装目录 xff0c 按照需求进行安装 xff0c 指定安装的版本 xff0c 灵活性比较大 2 源码安装
  • 正点原子mpu6050数据读取失败问题

    如果下载他们官方的程序都读不出来的话 看看你买的是stm32f407的V3版本吗 xff1f 这个版本是只有磁力计的官方代码 你用V3板跑他们的mpu的代码就会读不出来 xff0c 那个mpu6050的代码是已经停产的V2板子的
  • keil5 STM32F103 下载程序出错Flash Download failed - "Cortex-M3"

    1 背景 STM32F103单片机无法下载程序 最近在使用STM32单片机做项目 先是使用的H743单片机 xff0c 现在需要使用到F103单片机 H7烧写程序正常 xff0c 但是无法对F103烧写程序 错误为 xff1a Error
  • 略解总线带宽计算

    例1 xff1a 解 xff1a 时钟频率100MHz 也就是说一秒钟有100M个时钟周期 5个时钟周期传一个字 100M个时钟周期可以传100M 5 61 20M个字 也就是1秒钟可以传20M个字 一个字是16位 也就是2B 20M个字就
  • TX2(2): 安装JetPack L4T 3.1 (9003载板)

    参考官网教程 xff0c 其实官网教程已经挺详细 xff0c 主要看官网教程就行 http docs nvidia com jetpack l4t 3 1 index html developertools mobile jetpack l
  • String字符串编码格式转换(UTF8/GBK)

    1 转UTF8编码 string StdStringToUTF8 const string amp str int nwLen 61 MultiByteToWideChar CP ACP 0 str c str 1 NULL 0 wchar
  • 前缀树(Trie树)

    前缀树是一种用于快速检索的多叉树结构 xff0c 利用字符串的公共前缀来降低查询时间 xff0c 核心思想是空间换时间 xff0c 经常被搜索引擎用于文本词频统计 优点 xff1a 最大限度地减少无谓的字符串比较 xff0c 查询效率高 x
  • C++串口通信

    一 串口通信的基本原理 串口的本质功能是作为 CPU 和串行设备间的编码转换器 当数据从 CPU 经过串行端口发送出去时 xff0c 字节数据转换为串行的位 xff08 bit xff09 xff1b 在接收数据时 xff0c 串行的位被转
  • 死锁的四个必要条件以及处理策略

    一 什么是死锁 死锁是指两个或两个以上的进程 xff08 线程 xff09 在运行过程中因争夺资源而造成的一种僵局 例如 xff0c 某计算机系统中只有一台打印机和一台输入设备 xff0c 进程P1正占用输入设备 xff0c 同时又提出使用
  • EM算法简介

    1 简介 EM算法是一种迭代优化策略 xff0c 由于它的计算方法中每一次迭代都分两步 xff0c 其中一个为期望步 xff08 E步 xff09 xff0c 另一个为极大步 xff08 M步 xff09 xff0c 所以算法被称为EM算法
  • 三菱PLC MC协议

    1 MC协议的目的 xff1a 允许外部设备读写PLC内部寄存器 2 协议格式 xff1a 通讯方式有RS485和TCP IP两种 xff0c 通讯格式有很多种 xff1a 3E 3C 4C 4E帧格式 xff0c 通讯内容分为二进制和AS
  • find和find_if用法

    一 find的用法 STL容器中有很多find xff0c 比如说set xff0c map 他们内部都有内置的find函数 xff0c 一般情况下 xff0c 如果我们用到这些容器 xff0c 那么我们直接用它的内置find就可以了 xf
  • QTreeView节点拖放

    拖放操作分为拖动 Drag 和放置 Drop 两种操作 xff0c 当拖动时需要把拖动的数据进行存储 称为编码 xff0c 数据存储为QMimeData类型的对象 称为放置数据 xff0c 当执行放置操作时需要把存储的数据读取出来 称为解码
  • OOD七大原则

    1 单一职责原则 xff08 Single Responsibility Principle xff09 一个类或一个接口只有一个职责 xff0c 有且仅有一个原因引起变化 2 开闭原则 xff08 Open Closed Principl
  • 微服务探索之路05篇jenkins构建net6和vue docker镜像到Harbor自动更新k8s服务镜像

    从1 4篇已经学习了docker Harbor k8s的基本用法 接下来进阶一下使用jenkins结合起来做到自动部署项目 1 安装jenkins 1 1前提条件 docker环境 xff0c 可参考第01篇安装docker本文使用的是li
  • linux为用户添加sudo权限

    一 linux为用户添加sudo权限 用sudo时提示 34 xxx is not in the sudoers file This incident will be reported 其中XXX是你的用户名 xff0c 这是止当前用户没有
  • pixhawk多线程编程

    金错刀 pixhawk多线程程序编写 pixhawk源码多线程程序的编写 主要是针对pixhawk源码进行第二次开发的学习笔记 xff0c 记录下以便日后查阅 期望达到的目标 添加一个app应用 xff0c 在nsh的后台中运行该应用 xf
  • [视觉惯性导航系列]相机标定工具--kalibr

    前言 有很多博主推荐kalibr进行相机标定 我参考博主 纷繁中淡定 Kalibr标定Intel D435i相机 完成相机标定 但是kalibr在安装过程中会出现很多令人头秃的报错信息 综合了网上好多人的方法 才完成 本文做一点记录 本文不
  • C++ Exception

    Exception type Derived types scattered throughout different library headers bad alloc Exception thrown on failure alloca