C++ 中 Java 的 IllegalArgumentException 的等价物

发布于 2024-07-15 22:58:39 字数 237 浏览 2 评论 0原文

在 Java 中,如果方法的输入参数无效,我们可以抛出 IllegalArgumentException (其类型为 RuntimeException)。 在 C++ 中,没有检查异常和未检查异常的概念。 标准 C++ 中是否有类似的异常可用于指示运行时异常? 或者是否存在标准中没有的通用风格,但每个人在实践中都遵循这样的情况?

或者,我应该创建自己的自定义异常并抛出它吗?

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?

Or, should I just create my own custom exception and throw it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

网名女生简单气质 2024-07-22 22:58:39

与 Java 不同,C++ 没有“标准框架”,而只有一个小型(且可选)标准库。 此外,对于是否使用异常,C++ 程序员有不同的看法。

因此,你会发现不同的人有不同的建议:有些人喜欢使用标准库中的异常类型,有些库(例如 Poco)使用自定义异常层次结构(派生自 std::exception),而其他人则根本不使用异常(例如Qt)。

如果您想坚持使用标准库,则存在一个专门的异常类型:invalid_argument(扩展logic_error)。

#include <stdexcept>

// ...
throw std::invalid_argument("...");

供参考:以下是 stdexcept 中定义(和记录)的标准异常类型的概述:

exception
    logic_error
        domain_error
        invalid_argument
        length_error
        out_of_range
    runtime_error
        range_error
        overflow_error
        underflow_error

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.

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).

If you want to stick to the standard library, there exists a specialized exception type: 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:

exception
    logic_error
        domain_error
        invalid_argument
        length_error
        out_of_range
    runtime_error
        range_error
        overflow_error
        underflow_error
尹雨沫 2024-07-22 22:58:39

我总是使用 std::invalid_argument 来表示非法参数。

I always use std::invalid_argument for illegal arguments.

为人所爱 2024-07-22 22:58:39

std::domain_error 可能是您正在寻找的内容,但我怀疑很少有人使用它。 大多数人从 std::exception 派生自己的异常类型。

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.

如梦 2024-07-22 22:58:39

如果无效,您的意思是不满足方法的预期值,您可以抛出

std::logic_error 
or 
std::runtime_error.

如果您的意思是与强制转换相关的某些内容,例如一个对象无法转换为另一个对象 - 没有例外,并且不会自动抛出。

事实上它会。但仅限于dynamic_cast<> 关于参考文献。
它会抛出

std::bad_cast

我不确定你自己抛出这个是个好主意。

我更喜欢使用logic_error及其衍生物,以防有人传递了错误的参数,因为它是一个逻辑错误:程序员传递了错误类型的参数。

但更重要的是,我喜欢在这种情况下使用断言。 因为诸如向函数传递错误的值或类型之类的事情只有在开发期间才可以接受,并且在发布中应该避免此类检查。

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

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.

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.

拥抱影子 2024-07-22 22:58:39

您可以抛出标准异常或推出自己的异常。 您可能希望在抛出的异常中包含其他信息,这将是您自己执行此操作的一个很好的理由。

就我个人而言,我在我工作过的系统中还没有看到过这样的域检查。 这当然不具有普遍性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文