When objects in Java are partially initialized ( an exception occurs in the constructor )
<p>I've heard that it is possible to write some code like this</p>

<pre><code>SomeClass obj = null;

try {
    obj = new SomeClass();
} catch ( Exception e ) {
...
}
...
if ( obj != null ) { // here it holds true
...
}
</code></pre>

<p>Can somebody please explain, is that possible at all and under what conditions if we assume that constructor SomeClass <em>may</em> throw an Exception?</p>

<p>Another example:</p>

<pre><code>import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.Test;

public class Sample{

    static class A {

        public static A s;

        public A(Collection c) {
            c.add(this);
            s = this;
            throw new RuntimeException();
        }
    }

    @Test
    public void testResource() throws Exception {
        List l = new ArrayList();
        A a = null;
        try {
            a = new A(l);
            fail("Oops");
        } catch (Throwable e) {
        }
        assertTrue(l.size() == 1);
        assertNull(a);
        assertNotNull(A.s);
        assertNotNull(l.get(0));
    }

}
</code></pre>
