c++ - 在 C++ 中捕获类型错误

标签 c++ types try-catch typeerror

我如何检查结果的类型是否正确(int、float、double 等),然后抛出异常并捕获异常?

谢谢大家

弗拉德。

最佳答案

您能否详细说明是什么给了您“结果”,您可以从那里确定您需要什么,而且更有可能以更好的方式确定。

如果您真正想要的只是检查类型,请使用 typeid . 更多信息 here

按照 Daniel 的编辑帖子模型,在陈述其他内容后实际回答问题...

来 self 的其他评论:

You have to do this BEFORE you have just the result. Checking for overflow after is not a good idea. Do a check on the numbers before adding to see if they will overflow, or restrict input to be less than half the max value of the type

关于c++ - 在 C++ 中捕获类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1346989/

相关文章:

ios - 如何实现通用的 do-try-catch block 以捕获操作中抛出的所有错误

c++ - 在非模板化类析构函数中删除模板化类指针?

c++ - Visual Studio 编译器错误 : a call to a delegating constructor shall be the only member-initializer

Haskell,用于评估的 lambda 演算

c# - 具有 "2"后缀的 .NET 类型?

C# - Try-Catch-Finally 返回

c++ - 如何计算unordered_set c++中的冲突

c++ - Linux Mint 19 上的 CMake 3.11 无法找到静态 Boost 库

PHP 认为 P == 0

c++ - 如何避免使用 Try Catch (C++) 在整数值中输入字符串