Writing an API for checked subtraction
I guess your logic is fine. I just don't like what you do with the variable difference. You could return directly instead and save the variable and two lines.

In case the overflow s probable, you're wasting a lot of time because of the exception. You could do something like

long result = minuend - subtrahend;
return minuend &gt;= 0 | (minuend ^ result) &gt;= 0 ? result : Long.MIN_VALUE;


instead (condition adapted from LongMath.checkedSubtract). You could test it both against your original implementation (e.g. with a few random values) and against some known cases.



As suggested by user1071840, a simpler condition may be used:

return Long.MIN_VALUE + subtrahend &gt; minuend ? minuend - subtrahend : Long.MIN_VALUE;


Actually, rolfl's condition is even simpler.