It’s not always bad to catch RuntimeException. But even if your team decided not to catch RuntimeException, you can always catch it, register something, and then throw it away again. It will not change your application logic at all.
Almost all log libraries now have the ability to log various exception information (such as stacktrace and all nested exceptions) in addition to the log message.
public void doStuff(String param){
try {
process(param);
} catch(RuntimeException e) {
logger.error("Something weird happened while processing " + param, e);
throw e;
}
}
The following is an updated context information, thanks to Ralph Kleberhoff for pointing this outRuntimeException ( Exception) , .
(, , Ralf Kleberhoff), catch ( , catch ), Exception Exception cause .
public void doStuff(String param){
try {
process(param);
} catch(RuntimeException e) {
throw new RuntimeException("Something weird happened while processing " + param, e);
}
}
private void process(String value){
throw new IllegalStateException("Not implemented yet!");
}