在Java中,当对象实例化失败时会发生什么?
我来自 c++ 背景,我发现自己经常在 java 中这样做:
SomeClass sc=new SomeClass();
if(null!=sc)
{
sc.doSomething();
}
我想知道的是,如果构造函数由于某种原因失败(比如可能没有足够的内存),变量 sc 中将会有什么。我可以' 找不到直接的答案,我担心我只是在浪费时间,因为也许如果新的操作员失败,程序就会崩溃吗?
I come from a c++ background and I find myself constantly doing this in java:
SomeClass sc=new SomeClass();
if(null!=sc)
{
sc.doSomething();
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Java 规范语言第三版彻底涵盖了您的问题:
因此,
new
根本不可能表达式返回null
。无论返回什么,如果执行正常完成,无论实例化的类如何,都将始终是有效的实例。处理异常
一般来说,可能的异常通常使用
try-catch
块进行处理:根据您的情况,您可以考虑将
new SomeClass()
放入try< /code> 块带有相应的
catch (OutOfMemoryError e)
,但这非常不典型。除非您打算在发生这种情况时做一些有意义的事情,否则在大多数典型情况下,最好不要捕获
任何错误
可能在程序执行过程中发生。从文档中:
相关问题
另请参阅
The Java Specification Language 3rd Edition covers your question thoroughly:
So it's simply not possible for a
new
expression to returnnull
. Whatever is returned, if the execution completes normally, will always be a validinstanceof
whatever class was instantiated.Handling exceptions
Generally speaking, possible exceptions are usually handled with a
try-catch
block:In your case, you may consider putting
new SomeClass()
in atry
block with a correspondingcatch (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 notcatch
anyError
that may occur during your program execution.From the documentation:
Related questions
See also
如果构造函数失败,那么它将抛出异常或错误,并且您将无法进一步了解程序。具体来说,如果内存不足,您将收到
OutOfMemoryError
。If the constructor failed, then it would throw an exception or an error and you wouldn't get any further in the program. Specifically, if it ran out of memory, you would get an
OutOfMemoryError
.创建对象后,对象引用永远不会为
null
,因此不需要 null 检查,因为条件始终为 true。例如,如果构造函数中的该点没有足够的内存,则会抛出
OutOfMemoryError
并且构造函数将无法正常返回。An object reference is never
null
after creating an object, so the null check is unnecessary because the condition will always be true.If, for example, there is not enough memory at the point in the constructor, an
OutOfMemoryError
will be thrown and the constructor will not return normally.是的,你在浪费时间:-)。调用构造函数后,sc 保证为非空。如果构造函数失败,则会抛出异常,并且以下代码将永远不会运行。因此,以下内容在 Java 中是安全的:
不会抛出 NullPointerException。
Yes you are wasting your time :-). sc is guaranteed to be non-null after the constructor is called. If the constructor failed an exception would be thrown and the following code would never be run. Hence the following is safe in Java:
without throwing a NullPointerException.
不需要该
if
检查,有些 IDE 甚至会抱怨这是不必要的,因为它的计算结果始终为true
。new
失败的唯一原因是构造函数抛出异常,在这种情况下,您的if
检查无论如何都会被绕过。That
if
check is not needed, and some IDEs will even complain that it is unnecessary as it always evaluates totrue
. The only reason thatnew
would fail is if the constructor threw an exception, and in that case yourif
check will be bypassed anyways.抛出 OutOfMemoryError ,这将通常会导致虚拟机崩溃。
(可能被捕获为 Throwable)
您可能会发现 阻止并恢复Java OutOfMemory 错误 很有用。
An OutOfMemoryError is thrown which will usually crash the virtual machine.
(Could be catched as Throwable)
You might find Prevent and Recover from Java OutOfMemory Error useful.