等效于 C++ 中 Java 的 IllegalArgumentException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/681772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent of IllegalArgumentException of Java in C++
提问by Srikanth
In Java if an input argument to a method is invalid, we can throw an IllegalArgumentException(which is of type RuntimeException). In C++, there is no notion of checked and unchecked exceptions. Is there a similar exception in standard C++ which can be used to indicate a runtime exception? Or is there a common style not in the standard but everyone follows in practice for a situation like this?
在 Java 中,如果方法的输入参数无效,我们可以抛出一个IllegalArgumentException(类型为RuntimeException)。在 C++ 中,没有检查异常和未检查异常的概念。标准 C++ 中是否有类似的异常可用于指示运行时异常?或者有没有标准中没有但大家在实践中都遵循的共同风格,这样的情况?
Or, should I just create my own custom exception and throw it?
或者,我应该创建自己的自定义异常并抛出它吗?
回答by Ferdinand Beyer
Unlike Java, C++ does not have a "standard framework" but only a small (and optional) standard library. Moreover, there are different opinions under C++ programmers whether to use exceptions at all.
与 Java 不同,C++ 没有“标准框架”,而只有一个小型(可选)标准库。此外,还有在C ++程序员不同的意见是否使用例外可言。
Therefore you will find different recommendations by different people: Some like to use exception types from the standard library, some libraries (e.g. Poco) use a custom exception hierarchy (derived from std::exception), and others don't use exceptions at all (e.g. Qt).
因此,您会发现不同的人有不同的建议:有些喜欢使用标准库中的异常类型,有些库(例如 Poco)使用自定义异常层次结构(派生自 std::exception),而有些则根本不使用异常(例如Qt)。
If you want to stick to the standard library, there exists a specialized exception type: invalid_argument(extends logic_error).
如果你想坚持标准库,有一个专门的异常类型:invalid_argument(extends logic_error)。
#include <stdexcept>
// ...
throw std::invalid_argument("...");
For the reference: Here is an overview of standard exception types defined (and documented) in stdexcept:
供参考:以下是在 中定义(和记录)的标准异常类型的概述stdexcept:
exception
logic_error
domain_error
invalid_argument
length_error
out_of_range
runtime_error
range_error
overflow_error
underflow_error
回答by Srikanth
std::domain_error may be what you are looking for, but I suspect very few people use it. Most people derive their own exception types from std::exception.
std::domain_error 可能就是您要查找的内容,但我怀疑很少有人使用它。大多数人从 std::exception 派生他们自己的异常类型。
回答by rlbond
I always use std::invalid_argumentfor illegal arguments.
我总是std::invalid_argument用于非法参数。
回答by Mykola Golubyev
If by invalid you mean doesn't satisfied method expected values you can throw
如果无效,您的意思是不满足方法预期值,您可以抛出
std::logic_error
or
std::runtime_error.
If you mean something related to casts like one object can't be converted to another - there is no exception for that and it won't be thrown automatically.
如果你的意思是一些与强制转换相关的东西,比如一个对象不能转换为另一个 - 没有例外,它不会自动抛出。
In fact it will.But only for dynamic_cast<> on references. It will throw
事实上它会。但仅适用于 dynamic_cast<> 上的引用。它会抛出
std::bad_cast
I am not sure it is a good idea to throw this one by your own.
我不确定自己扔这个是个好主意。
I prefer to use logic_error and its derivatives in case someone passed wrong parameter because it is a logic error: programmer passed wrong type of argument.
我更喜欢使用 logic_error 及其衍生物,以防有人传递错误的参数,因为这是一个逻辑错误:程序员传递了错误类型的参数。
But more of all I like to use assert in such cases. Because such things like passing wrong values or types to your function can be acceptable only during development and such checks should be avoided in the release.
但更重要的是,我更喜欢在这种情况下使用断言。因为诸如将错误的值或类型传递给您的函数之类的事情只能在开发过程中接受,并且在发布中应避免此类检查。
回答by David Thornley
You can throw a standard exception or roll your own. You may want to include additional information in the exception you're throwing, and that would be a good reason to do your own.
您可以抛出标准异常或推出自己的异常。您可能希望在您抛出的异常中包含其他信息,这将是您自己做的一个很好的理由。
Personally, I haven't seen such domain checking in systems I've worked on. It certainly isn't universal.
就个人而言,我还没有在我工作过的系统中看到这样的域检查。它当然不是通用的。

