Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

try/catch vs null check in java

Tags:

java

Sometimes I face I must write a piece of code like this (usually it have more nested if and more complex structure but for the example is enought)

public void printIt(Object1 a){   if (a!=null){      SubObject b= a.getB();      if (b!=null){          SubObject2 c=b.getC();          if(c!=null){              c.print();          }      }   } } 

when I dont need to know what failed and if something is null do nothing, an approach could be

public void printIt(Object1 a){     try{       a.getB().getC().print();     }catch (NullPointerException e) {     } } 

Is there something wrong in this second form like performance or other kind of issues?

like image 530
Addev Avatar asked Mar 18 '12 12:03

Addev


People also ask

Is it good practice to catch NullPointerException?

It is generally a bad practice to catch NullPointerException. Programmers typically catch NullPointerException under three circumstances: The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem.

What is difference between try and catch in Java?

The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

Is it good to use try-catch in Java?

try/catch clause is used for things that goes wrong that are outside of your control and not in the normal program flow. For example, trying to write to a file and the file system is full? That situation should typically be handled with try/catch .

What can I use instead of try-catch in Java?

But anyhow, even without them, Vavr Try is a real alternative for Java try-catch blocks if you want to write more functional-style code.


2 Answers

The exception version (similar to chains using Groovy's safe-navigation operator ?.) makes it really easy to take the Law of Demeter (or as I call it, Demeter's Strongly-Worded Suggestion) and make it your plaything for the night.

Similarly, deeply-nested if-statements leads to difficult-to-read code, and underneath it all, the same "violation" exists, and the cyclomatic complexity of such methods is high.

public void printIt(Object1 a) {     if (null == a) {         return;     }      SubObject b = a.getB();     if (null == b) {         return;     }      SubObject2 c = b.getC();     if (null == c) {         return;     }      c.print(); } 

I'd rather see LAMs (Little Auxiliary Methods) in appropriate places that encapsulate the checks and pretty much eliminate the need for the question altogether.

like image 166
Dave Newton Avatar answered Sep 16 '22 18:09

Dave Newton


Yes. The second version will have terrible performance.

Don't use exceptions for normal control flow. Effective Java item 57: use exceptions only for exceptional situations.

==UPDATE==

Even ignoring performance issues (exceptions are faster than they once were, according to my benchmark, but not nearly as fast as a simple if check), it's really code-smelly to use exceptions for standard program flow like this. JVM bytecode has special optimizations it can do for null checks, even in if statements. The first code sample is vastly preferred.

like image 21
Louis Wasserman Avatar answered Sep 18 '22 18:09

Louis Wasserman