13

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 :) ) ?

1
  • Only if you catch your exception right there in the constructor. I mean if it won't get thrown further to the caller. Commented May 6, 2011 at 10:20

4 Answers 4

31

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.

Sign up to request clarification or add additional context in comments.

3 Comments

wow, good answer. Never thought of object construction like this. I know a similar problem where the subclass (re)implements some superclass method, and thus could potentially expose partially constructed objects. However, the C instance in this case is fully constructed - it is not possible to (for example) add a final field to the class C, initialize it as a field initialization, and yet not have that field be initialized in this case.
I have write the same test :) Thanks.
And not just see that object is not null, we can CALL method from C.obj;
4

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.

2 Comments

-1: The object is not assigned, but it is still created, see the other answer.
Daniel, who disagrees? The thing I wrote states the same: assignment does not take place.
2

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. 

2 Comments

Yes I know, I have tested this situation, but was interested to know, What will be or Can be the "magic" situation when object is not null.
As Gasan said above "only if you catch your exception right there in the constructor. I mean if it won't get thrown further to the caller" I hope it makes sense
-1
public Test() { name = "John"; try { // exception // init some other data. } catch(AnyException e) { // catch } } 

The above code makes sense as per your expectation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.