c++ - 哪种数据类型不能在 C++ 程序中作为异常抛出

标签 c++ exception

可以抛出类型为 int、float、long 或自定义数据类型(如类和结构)的异常。但是在 C++ 中哪种数据类型不能作为异常抛出?

最佳答案

异常不能抛出不完整的类型:

§ 15.1

If the temporary is an lvalue and is used to initialize the variable named in the matching handler (15.3). If the type of the exception object would be an incomplete type or a pointer to an incomplete type other than (possibly cv-qualified) void the program is ill-formed. Except for these restrictions and the restrictions on type matching mentioned in 15.3, the operand of throw is treated exactly as a function argument in a call (5.2.2) or the operand of a return statement.

§ 15.5

When the thrown object is a class object, the copy/move constructor and the destructor shall be accessible, even if the copy/move operation is elided (12.8).

15.3.1 处理异常

处理程序中的异常声明描述了可能导致进入该处理程序的异常类型。 异常声明不应表示不完整类型或右值引用类型。异常声明不应表示指向不完整类型的指针或引用,void*、const void*、volatile void* 或 const volatile void* 除外。

关于c++ - 哪种数据类型不能在 C++ 程序中作为异常抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17785918/

相关文章:

java - Java swing 应用程序 Action 监听器中的异常

java - 当引用的方法抛出异常时,如何以一个方法作为参数调用另一个方法?

c++ - 链接列表(从 'Node<int>*' 到 'int' [-fpermissive]| 的无效转换)

c++ - undefined reference ,编译错误

c++ - 当基类具有 protected 析构函数时创建 unique_ptr<Base>

c++ - std::atomic 不受 clang 支持?

C++ 仅在满足模板类型的条件时才创建类的实例

java - 堆栈跟踪显示错误位置的异常

c++ - 错误 2296 : '^' : illegal , 左操作数的类型为 'double'

c++ - 在双重继承的情况下如何处理非标准构造函数