C ++/GCC:如何在编译时检测未处理的异常 [英] C++/GCC: How to detect unhandled exceptions in compile time

查看:152
本文介绍了C ++/GCC:如何在编译时检测未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介: 在Java中,如果没有捕获到异常,则代码甚至都不会编译,并且编译器会在未处理的异常时崩溃.

Introduction: In Java, if you do not catch an exception, your code doesn't even compile, and the compiler crashes on unhandled exception.

问题: 在这种情况下,是否有办法告诉GCC像Java那样严格",并针对未处理的异常引发错误或至少发出警告?

Question: Is there a way to tell GCC to be "strict" as Java in this case, and to raise an error or at least a warning on unhandled exception?

如果没有-是否有可以突出显示诸如警告之类的IDE的IDE(请用于Unix)?

If not - are there IDEs (for Unix, please) that can highlight such cases as a warning?

推荐答案

在C ++中是不可能的.异常说明是功能声明的一部分,而不是其 type 的一部分.任何间接调用(通过指针或虚拟调用)都将完全擦除有关异常的所有信息.

It is not possible in C++. Exception specification is a part of a function declaration but not a part of its type. Any indirect call (via pointer or virtual call) just completely wipes any information about exceptions.

在C ++ 11中始终不推荐使用异常规范,而推荐使用noexcept,因此,任何编译器都不太可能去增强此语言功能.

Exception specifications are deprecated anyway in C++11 in favour of noexcept, so it is unlikely any compiler would bother to enhance this language feature.

这篇关于C ++/GCC:如何在编译时检测未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆