为日志记录目的捕获RuntimeException是不好的做法吗? [英] Is it bad practice to catch RuntimeException for logging purposes?

查看:273
本文介绍了为日志记录目的捕获RuntimeException是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现捕获RuntimeException通常被认为是不好的做法,因为它们无法纠正,通常是程序员错误。

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).

现在这个想法已经开始在应用程序的最高级别开始捕获和记录RuntimeExceptions,因此我们可以更有效地修复出现这样的流失问题。

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.

像所有商品一样Java团队,我们有一个可爱的热心叔叔鲍勃粉丝,绝对禁止我们这样做。

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?

推荐答案

抓住的RuntimeException 的。但即使你的团队决定不捕获 RuntimeException ,你也可以随时捕获它,记录一些内容然后重新抛出它。它根本不会改变你的应用程序逻辑。

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.

现在几乎所有的logger库都有可能记录有关异常的不同细节(比如堆栈跟踪和所有嵌套异常) )除了日志消息。

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;
  }
}



以下是关于上下文的更新,感谢 Ralf Kleberhoff 指出

最好记录一条关于 RuntimeException 的消息(或任何其他异常 )仅在应用程序的顶级,以避免在日志中有关同一异常的重复消息。

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.

如果你只是想添加一些上下文(比如参数值,如 Ralf Kleberhoff 提到)并且它不是应用程序顶级 catch (并且您确定,顶级 catch 实际存在),最好创建一个新的异常并将原始异常添加为导致进入新的。

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!");
}

这篇关于为日志记录目的捕获RuntimeException是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆