You can always make some factory class
class DoubleFactory{
public static double tryParseDouble(final String number){
double result;
try {
result = Double.parseDouble(number);
}
catch (NumberFormatException e) {
result = 0.0;
}
return result;
}
}
But there is huge problem with that. Your program will continue its normal flow, but some of your model classes will be 'broken'. And after some other operation this 'default' value will pop-up, and broke other , and other. And the worst of all, you will not see the exception leading for these broken results. At least you can
catch (NumberFormatException e) {
//add exception logging here, something like
logger.info(e.getMessage());
result = 0.0;
}
but the result will be the same - operations using default 0.0 (or -1.0 or whatever) value leading to some unrecoverable state.