I am not advocating catching a NullPointerException
as it is always a code smell/lazy approach. But, consider you want to access a field deep inside nested beans.
Defensive NPE check
if ( null != a && null != a.b() && null ! = a.b().c() && null != a.b().c().d() )
{
doSomething( a.b().c().d().e );
}
Lazy NPE Check
try
{
doSomething( a.b().c().d().e );
}
catch(NullPOinterException npe)
{
}
Two questions:
Performance
At what depth [a,b,c....z as in the example above] is the heavier weight try/catch more efficient than Defensive NPE check?
Readability
At what depth [a,b,c....z as in the example above] is the try/catch more readable than multiple &&?
Please don't answer 're-factor the nested beans' :-)