Q:

Try Catch Performance Java

How much longer (in nanoseconds) does a try-catch take when catching an exception rather than doing a check (assuming message has HashMap type performance for lookup)?
    try {
        timestamp = message.getLongField( MessageField.TIMESTAMP );
    } catch (MissingDataException e) {
        //Not all messages contain this field
    }

vs
if (message.contains(MessageField.TIMESTAMP))
    timestamp = message.getLongField( MessageField.TIMESTAMP );

A:

In short, the check is way faster. You should use the check because:

Exceptions are EXPENSIVE! A stack trace must be created (if used, eg logged etc) and special flow control handled
Exceptions should not be used for flow control - Exceptions are for the "exceptional"
Exceptions are the code's way of saying "I can't handle this situation and I'm giving up... you deal with it!", but here you can handle it... so handle it

A:

The answer is "much longer". Exceptions are slow compared to a check due to time to build the stacktrace.
As a general point using exceptions to control program flow is a bad idea as it clutters the code up.  Always do the check and leave exceptions for when "exceptional" things happen.

A:

Another answer is "Who cares!". It's wrong!
In any case, if you want to benchmark it, use https://github.com/google/caliper

