C ++的new运算符可以在现实生活中抛出异常吗? [英] Can the C++ `new` operator ever throw an exception in real life?
问题描述
new
运算符可以在现实生活中引发异常吗?
Can the new
operator throw an exception in real life?
如果是这样,除了杀死我的应用程序之外,我是否还有其他处理此类异常的选项?
And if so, do I have any options for handling such an exception apart from killing my application?
更新:
在没有内存的情况下,是否有任何现实世界中的重载应用程序检查故障并恢复?
Do any real-world, new
-heavy applications check for failure and recover when there is no memory?
- How often do you check for an exception in a C++ new instruction?
- Is it useful to test the return of "new" in C++?
- Will new return NULL in any case?
推荐答案
new运算符和new []运算符应抛出std::bad_alloc
,但这并非总是如此,因为有时可以覆盖该行为.
The new operator, and new[] operator should throw std::bad_alloc
, but this is not always the case as the behavior can be sometimes overridden.
一个人可以使用std::set_new_handler
,突然之间可能会发生与抛出std::bad_alloc
完全不同的事情.尽管标准要求用户要么使内存可用,中止或抛出std::bad_alloc
.但是,当然并非如此.
One can use std::set_new_handler
and suddenly something entirely different can happen than throwing std::bad_alloc
. Although the standard requires that the user either make memory available, abort, or throw std::bad_alloc
. But of course this may not be the case.
免责声明:我不建议这样做.
Disclaimer: I am not suggesting to do this.
这篇关于C ++的new运算符可以在现实生活中抛出异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!