7

What costs for sure less time for execution between the two options:

A:

if(something!=null){ ... }else{ //log } 

or:

B:

try{ something.getField();... }catch(Exception e){ //log } 
4
  • 2
    As an aside you should never catch the base Exception. As others have said the Exception option is more expensive, but if you really need to catch an exception here you should catch NullReferenceException. Commented Jun 18, 2014 at 12:06
  • ... When you catch an exception, you're stating that you expected this exception, and you know how to deal with it. The code above implies that you know to deal with any situation. Commented Jun 18, 2014 at 12:16
  • @BlackKnight, there is no NullReferenceException in java unlike c#, and you should never catch directly a NPE unless you have to make some workaround. Catching Exception is fine as long as you can ensure you can live with the lack of result/partial completion/etc. Commented Jun 18, 2014 at 17:10
  • @bestsss That's right, I guess I still have my C# brain in. NullPointerException would be the Java equivalent. Commented Jun 20, 2014 at 11:05

3 Answers 3

16

if definitely.

Throwing an exception is a costly operation and this is not the purpose of Exception.

The purpose of Exception is to catch exceptional condition that may arise at runtime but you shouldn't code to generate exception to make that decision.

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

7 Comments

One exception to this is maybe trying to find out if a string is a number
@blank: y u no regex?
this is completely correct. and you can verify this if you use your debugger. look at the exception that is created by your code, it stores some good amount of information. this has to be created at a time, and this is where your code becomes more expensive.
@blank Oh, by the way, when the exception is never thrown (When Integer.parseInt() receives String qualified as Integer) Using a try{} block DOES have better performance. But when the exception is thrown more than a dozen times, regex performance is drastically better. So it seems like this is more algorithm-ended, but preferably use regex anyway. :D
if definitely - this is untrue for null checks (or actually lack of), null deference can be handled by the hardware - even if the if (x!=null) is perfectly predicted by the hardware it still takes 1-2cycles. While you should not be catching NPE to deal with the case, exceptions are not so slow either - esp. when the compiler can prove the stack trace is unneeded.
|
10

Without even having to benchmark: Exception are ALWAYS way more expensive than programming defensively and using ifs as null-guard etc. Exceptions are always more expensive (several orders of magnitude), because the stack trace has to be generated.

Relevant SO question with benchmark: How slow are Java exceptions?

Comments

5

If emits a single branch. Throwing an exception "unrolls" the stack, which takes much longer.

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.