Java 和 C#,可能还有许多其他语言,都有一个预定义的异常类,当在不应该使用的地方使用空参数时会抛出该异常类.C ++中有类似的东西吗?如果没有,我可以使用其他预定义的异常还是应该定义自己的异常?
取消引用 NULL 指针是 C++ 中的未定义行为 - 这意味着代码看起来可以工作.不保证会抛出异常.您可以使用
std::invalid_argument
>
异常(为其提供一个有意义的值 - p 为 NULL"
),但您必须自己进行检查.
Both Java and C#, and probably many other languages too, have a predefined exception class that is thrown when a null parameter is used where it should not. Is there anything similar in C++? If not, is there another predefined exception I can use or should I define my own one?
Dereferencing a NULL pointer is undefined behaviour in C++ - which means the code can appear to work. An exception isn't guaranteed to be thrown. You can use the
std::invalid_argument
exception (provide a meaningful value to it - "p is NULL"
), but you'll have to do the check yourself.
这篇关于是否有一个相当于 NullPointerException 的 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!