当遇到不是来自 STL 的未捕获异常时,有没有办法打印其信息?
Is there any way to print its information when met an uncaught exception that is not from STL?
当编译器遇到来自 STL 的异常时,如std::out_of_range
:
int main(int argc, char *argv[]) {
throw std::out_of_range("There is an exception!");
}
控制台将显示以下消息:
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: There is an exception!
所以我写了一个异常类:
class Exception {
protected:
const char *msg;
public:
Exception(const char *msg) noexcept : msg(msg) {}
Exception(const string &msg) noexcept : msg(msg.c_str()) {}
const char *what() const noexcept {
return this->msg;
}
};
但是,投掷Exception
不会得到任何信息:
int main(int argc, char *argv[]) {
throw Exception("There is an exception!");
}
控制台消息 :
libc++abi.dylib: terminating with uncaught exception of type Exception
有没有办法制作控制台显示:
libc++abi.dylib: terminating with uncaught exception of type Exception: There is an exception!
编译器 :Apple LLVM version 9.1.0 (clang-902.0.39.2)
如果从std::exception
公开派生异常类,则编译器提供的异常处理程序将能够打印异常的详细信息。
相关文章:
- 有没有办法搜索向量的元素,<String>然后检查它是否包含特定的字符,如果它确实打印了它
- 有没有一种方法可以使用图形在C++中逐个字母地打印控制台中的文本
- 当遇到不是来自 STL 的未捕获异常时,有没有办法打印其信息?
- 有没有办法用单行逻辑代码打印金字塔星形图案?面试官向我的朋友问了这个问题
- 有没有更简单的方法可以从用户那里获取三个数字并按升序打印它们?
- 有没有办法将字符串打印为文字?
- 有没有办法知道C++中将打印到标准输出的大小?
- 有没有办法打印包含当前文件的文件的文件名
- 有没有办法更快地打印2D阵列
- 有没有办法打印所有类数据
- 有没有办法在我的程序中打印所有声明的变量
- GDB有没有办法在不省略模板参数的情况下打印类型
- 有没有办法在不重载<<运算符的情况下打印出 std::list 的内容?
- 有没有一种方法可以在Netbeans中的断点处打印变量的值
- 有没有一种方法可以打印对象的位表示
- 除了显示平均,最高和最低温度之外,有没有办法打印出整个排序列表
- 有没有办法让 GDB 打印一些东西来告诉我一行已经执行,而不会停止
- 有没有办法创建一个公共输出流对象以在控制台上打印并打印到 c++ 中的文件
- 有没有办法通过 int 指针一次打印整数
- 有没有通过打印字符来播放动画的c程序?