在Java中,当对象无法实例化时会发生什么? [英] In Java what happens when an object fails to be instantiated?

查看:258
本文介绍了在Java中,当对象无法实例化时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自c ++背景,我发现自己经常在java中这样做:

I come from a c++ background and I find myself constantly doing this in java:

SomeClass sc=new SomeClass();

if(null!=sc)
{
    sc.doSomething();
}

我想知道的是如果构造函数将在变量sc中出现什么由于某种原因失败(可能没有足够的内存)。我可以'
t找到一个直接的答案,我担心我只是在浪费我的时间,因为如果新的操作员失败会导致程序崩溃吗?

What I want to know is what will be in the variable sc if the constructor fails for some reason (like maybe not enough memory). I can' t find a straight answer, and I am worried that I am just wasting my time because maybe if the new operator fails would the program just crash anyway?

推荐答案

Java规范语言第3版彻底涵盖了您的问题:

The Java Specification Language 3rd Edition covers your question thoroughly:


< h3> 12.5创建新的班级实例

每当创建一个新的类实例时,就为它分配内存空间,为类类型中声明的所有实例变量提供空间,并在每个超类中声明所有实例变量类类型,包括可能隐藏的所有实例变量。如果没有足够的可用空间来为对象分配内存,那么使用 OutOfMemoryError 。否则,新对象中的所有实例变量(包括在超类中声明的变量)都将初始化为其默认值。

12.5 Creation of New Class Instances

Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden. If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values.

在作为结果返回对新创建的对象的引用之前,处理指示的构造函数以使用以下过程初始化新对象:[...]

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure: [...]

因此, new 表达式根本不可能返回。无论返回什么,如果执行正常完成,将始终是一个有效的 instanceof 无论实例化什么类。

So it's simply not possible for a new expression to return null. Whatever is returned, if the execution completes normally, will always be a valid instanceof whatever class was instantiated.

一般来说,可能的异常通常用 try-catch block:

Generally speaking, possible exceptions are usually handled with a try-catch block:

String someString = askFromUser();
try {
   int num = Integer.parseInt(someString);
   doSomethingWith(num);
} catch (NumberFormatException e) {
   complainAboutIt();
}

在你的情况下,你可以考虑把新的SomeClass ()尝试块中,带有相应的 catch(OutOfMemoryError e),但是这个是非常不典型的。除非你计划在发生这种情况时做一些有意义的事情,否则在大多数情况下,最好不要 catch 任何 错误

In your case, you may consider putting new SomeClass() in a try block with a corresponding catch (OutOfMemoryError e), but this is highly atypical. Unless you plan to do something meaningful when this happens, in most typical scenarios it's best to not catch any Error that may occur during your program execution.

来自文档:


错误 Throwable 的子类,表示合理的应用程序不应该尝试到<$ c的严重问题$ C>捕获。大多数此类错误都是异常情况。

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.

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

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.



相关问题



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