I have a final member data:
public final Foo foo;
in the constructor, foo is initialized as follow:
foo = new Foo();
Now, unfortunately, Foo’s constructor might throw an exception:
try {
foo = new Foo();
} catch (Exception e) {
e.printStackTrace();
}
But now compiler complains that foo might not be initialized, which is true if Foo’s constructor throws an exception. But if I put foo = null inside the catch braces, it complains that foo might have been initialized.
Foo is a third-party library that I cannot modify.
So, what’s the most graceful way of handling this?
I make no comment about whether it makes sense to continue with construction of your outer object if the inner object’s constructor has failed…