如果新的失败怎么办? [英] What if new fails?

查看:115
本文介绍了如果新的失败怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++和C#中,当新的无法分配内存时,它会抛出异常。

In C++ and C# when new not able to allocate enought memory it throws exception.

我找不到有关Java中新行为的任何信息。
那么如果新的Java失败(内存不足)会发生什么?

I couldn't find any information about new's behavior in Java. So what will happen if new fails in Java (not enough memory)?

推荐答案

假设具体意味着内存分配失败,那么它应抛出 的OutOfMemoryError

Assuming you specifically mean failure of the memory allocation, then it should throw OutOfMemoryError


当Java虚拟机因内存不足而无法分配对象时抛出,垃圾收集器不再提供更多内存。

Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.

错误的所有子类一样,它通常是不可恢复的条件,即使技术上你可以抓住它:

Like all subclasses of Error, it's usually a non-recoverable condition, even if technically you can catch it:


错误是Throwable的一个子类,表明合理的应用程序存在严重问题不应该试图抓住。大多数此类错误都是异常情况。 ThreadDeath错误,虽然是正常条件,但也是Error的子类,因为大多数应用程序不应该尝试捕获它。

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

不需要声明方法它的throws子句可能在方法执行期间抛出但未捕获的任何子类Error,因为这些错误是不应该发生的异常情况。

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.

这篇关于如果新的失败怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆