Can the C++ `new` operator ever throw an exception in real life?

前端 未结 18 2169
隐瞒了意图╮
隐瞒了意图╮ 2021-01-30 08:40

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?

18条回答
  •  死守一世寂寞
    2021-01-30 09:28

    In many cases there's no reasonable recovery for an out of memory situation, in which case it's probably perfectly reasonable to let the application terminate. You might want to catch the exception at a high level to display a nicer error message than the compiler might give by default, but you might have to play some tricks to get even that to work (since the process is likely to be very low on resources at that point).

    Unless you have a special situation that can be handled and recovered, there's probably no reason to spend a lot of effort trying to handle the exception.

提交回复
热议问题