c++ - C++ 异常抛出语法
问题描述
我目前正在阅读一本 C++ 书籍,遇到了这段我不知道如何解释的代码:
#include <exception>
#include <memory>
struct empty_stack: std::exception // don't know what the code after : means
{
const char* what() const throw(); //don't understand this line either
};
解决方案
struct empty_stack: std::exception // don't know what the code after : means
这意味着empty_stack
公共继承自std::exception
which 是标准异常的基类。
注意:如果不指定继承类型,则默认继承类型取决于继承类型。如果private
继承类型是class
,public
如果继承类型是struct
。
const char* what() const throw(); //don't understand this line either
这意味着该what()
函数不会修改它所属的类的非可变成员并且不会引发任何异常。throw()
但是在结尾处表示它不会抛出 有点误导。
因此,从 C++11 开始,我们有了说明noexcept
符。在像下面这样的函数声明中使用 this 意味着该函数被声明为不抛出任何异常。
const char* what() const noexcept;
注意:throw()
已弃用,将在 C++20 中删除。
推荐阅读
- android - 在失败时收到此错误:格式错误的 URL。基础:https://texttospeech.googleapis.com/v1beta1/,相对:text:synthesize
- java - 从 xml 文件中读取数据并存储为键值对
- linux - 在 linux 上的简单(操作系统)文件上制作 FAT12 文件系统
- java - 用Java打印星形图案
- php - 单击删除按钮时不显示删除确认框
- ios13 - 迁移到 xcode 11 后如何修复警告
- javascript - 点击按钮时firebase删除数据库上的ID
- pine-script - 将标签或标签添加到绘图线
- python - import .package 和 import package 的区别
- unix - 借助固定字符串开关 grep 文件中的多个单词