可以抛出类型为 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/