Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad practice to catch RuntimeException for logging purposes?

I've come across the fact that catching RuntimeException is generally considered bad practice because they can't be corrected and usually are programmer errors.

However, we have an (insanely) large application where changes in any part can have unforeseen consequences (Yes, this a problem in and of itself).

Now the idea has come up to start catching and logging RuntimeExceptions at the application's top level so we can be more efficient in fixing such bleed issues as they come up.

Like every good Java team, we have a lovely zealous Uncle Bob follower who is absolutely forbidding us from doing it.

How bad is it really to do this? Are there really no cases in which this is okay or even recommended?

like image 314
Weckar E. Avatar asked Aug 10 '17 08:08

Weckar E.


2 Answers

Catching RuntimeExceptions is not a problem, they are Exceptions so they can be caught and properly processed. If the Java developers would have wanted you to not catch RuntimeExceptions, they would have named it RuntimeError.

What is bad is to catch RuntimeExceptions, discard them silently and continue to run as if nothing ever happened. RuntimeExceptions are there to notify the developer/user of critical issues and situations where the program clearly left the expected state. At a minimum this should be logged, and you should try to get the program back into a comfortable state.

If you can continue after such a major failure totally depends on your application. Web-Servers i.E. commonly catch all exceptions and errors, and just restart the appropriate Servlet so they can continue to serve requests.

like image 75
TwoThe Avatar answered Sep 26 '22 23:09

TwoThe


It's not always bad to catch RuntimeException's. But even if your team decided to not catch RuntimeException's, you can always catch it, log something and then re-throw it. It won't change your application logic at all.

Almost all logger libraries nowadays have possibility to log different details about the exception (like a stacktrace and all nested exceptions as well) in addition to log message.

public void doStuff(String param){
  try {
    process(param);
  } catch(RuntimeException e) {
    logger.error("Something weird happened while processing " + param, e);
    throw e;
  }
}


Below is an update, about the context, thank you Ralf Kleberhoff for pointing on that

It's better to log a message about RuntimeException (or any other Exception) only on a top levels of your application, to avoid duplicate messages about the same exception in the log.

If you just want to add some context (like a parameter value, as Ralf Kleberhoff mentioned) and it's not the application top level catch (and you are sure, that top level catch actually exists ), it's better to create a new Exception and add original Exception as a cause into the new one.

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!");
}
like image 23
Maxim Ponomarev Avatar answered Sep 24 '22 23:09

Maxim Ponomarev