Java:构造函数中抛出异常,我的对象仍然可以创建吗?

发布于 2024-11-06 02:52:37 字数 231 浏览 2 评论 0原文

你能告诉我在构造函数中抛出异常并且对象不为空的情况下吗?我的意思是对象的某些部分被创建,而另一部分则没有。像这样,

public Test(){
name = "John";
// exception
// init some other data.
}

我理解在这种情况下对象 Test 将为 null,但可能是对象 test 不能为 null 的情况(删除异常块不回答:))?

Could you tell me can be some case when exception is throwing in constructor and object is not null. I mean some part of object is created and another is not.Like this

public Test(){
name = "John";
// exception
// init some other data.
}

I understand in this sitiation object Test will be null, but Can be situation that object test cannot be null (delete block of exception not answer :) ) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黑寡妇 2024-11-13 02:52:37

如果类实例创建表达式的限定符和参数的计算正常完成,并且有足够的空间来创建该对象,则类实例创建表达式始终会创建一个新对象。构造函数是否抛出异常并不重要;仍然创建了一个对象。但是,在这种情况下,类实例创建表达式不会正常完成,因为它会传播异常。

但是,您仍然可以获得对新对象的引用。请考虑以下情况:

public class C {
    static C obj; // stores a "partially constructed" object
    C() {
        C.obj = this;
        throw new RuntimeException();
    }
    public static void main(String[] args) {
        C obj;
        try {
            obj = new C();
        } catch (RuntimeException e) {
            /* ignore */
        }
        System.out.println(C.obj);
    }
}

这里,在引发异常之前,对新对象的引用存储在其他位置。如果运行这个程序,您将看到该对象确实不为空,尽管它的构造函数没有正常完成。

A class instance creation expression always creates a new object if the evaluation of its qualifier and arguments complete normally, and if there is space enough to create the object. It doesn't matter if the constructor throws an exception; an object is still created. The class instance creation expression does not complete normally in this case, though, as it propagates the exception.

However, you can still obtain a reference to the new object. Consider the following:

public class C {
    static C obj; // stores a "partially constructed" object
    C() {
        C.obj = this;
        throw new RuntimeException();
    }
    public static void main(String[] args) {
        C obj;
        try {
            obj = new C();
        } catch (RuntimeException e) {
            /* ignore */
        }
        System.out.println(C.obj);
    }
}

Here, a reference to the new object is stored elsewhere before the exception is thrown. If you run this program, you will see that the object is indeed not null, though its constructor did not complete normally.

◇流星雨 2024-11-13 02:52:37

没有。看一下客户端代码:

Test myObj = null;
try {
 myObj = new Test();
} catch(MyException e) {
  System.out.println("" + myObj);
}

这里,当发生异常时,不执行'='操作。您的代码直接进入 catch 块,并且 myObj 保持 null

No. Look at the client code:

Test myObj = null;
try {
 myObj = new Test();
} catch(MyException e) {
  System.out.println("" + myObj);
}

Here, when exception occurs, the '=' operation is not executed. Your code goes straight to the catch block and myObj stays null.

默嘫て 2024-11-13 02:52:37

不会。如果对象实例化过程中发生异常,则不会创建该对象。

无论如何,你会写吗?

MyObject obj = new MyObject();
// This code will not be reachable in case of an Exception

或者:

MyObject obj = null;
try {
    obj = new MyObject();
} catch (AnyException e) {
}
// Here, either obj is created correctly, or is null as an Exception occurred.

No. If exception occurs during the instantiation of the object, it will not be created.

Anyway, you would you write it?

MyObject obj = new MyObject();
// This code will not be reachable in case of an Exception

or:

MyObject obj = null;
try {
    obj = new MyObject();
} catch (AnyException e) {
}
// Here, either obj is created correctly, or is null as an Exception occurred.
哀由 2024-11-13 02:52:37
public Test() {  
    name = "John"; 
    try {
        // exception 

        // init some other data.
    } catch(AnyException e) {
        // catch
    }
}

上面的代码符合您的期望。

public Test() {  
    name = "John"; 
    try {
        // exception 

        // init some other data.
    } catch(AnyException e) {
        // catch
    }
}

The above code makes sense as per your expectation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文