Should I catch exceptions only to log them?

2020-02-17 05:21发布

Should I catch exceptions for logging purposes?

public foo(..)
{
   try
   {
     ...
   } catch (Exception ex) {
     Logger.Error(ex);
     throw;
   }
}

If I have this in place in each of my layers (DataAccess, Business and WebService) it means the exception is logged several times.

Does it make sense to do so if my layers are in separate projects and only the public interfaces have try/catch in them? Why? Why not? Is there a different approach I could use?

15条回答
SAY GOODBYE
2楼-- · 2020-02-17 05:45

This Software Engineering Radio podcast is a very good reference for best practices in error handling. There are actually 2 lectures.

查看更多
叼着烟拽天下
3楼-- · 2020-02-17 05:46

My method is to log the exceptions only in the handler. The 'real' handler so to speak. Otherwise the log will be very hard to read and the code less structured.

查看更多
ゆ 、 Hurt°
4楼-- · 2020-02-17 05:47

Unless you are going to change the exception, you should only log at the level where you are going to handle the error and not rethrow it. Otherwise your log just has a bunch of "noise", 3 or more of the same message logged, once at each layer.

My best practice is:

  1. Only try/catch in public methods (in general; obviously if you are trapping for a specific error you would check for it there)
  2. Only log in the UI layer right before suppressing the error and redirecting to an error page/form.
查看更多
Ridiculous、
5楼-- · 2020-02-17 05:48

You will want to log at a tier boundary. For example, if your business tier can be deployed on a physically separate machine in an n-tier application, then it makes sense to log and throw the error in this way.

In this way you have a log of exceptions on the server and don't need to go poking around client machines to find out what happened.

I use this pattern in business tiers of applications that use Remoting or ASMX web services. With WCF you can intercept and log an exception using an IErrorHandler attached to your ChannelDispatcher (another subject entirely) - so you don't need the try/catch/throw pattern.

查看更多
小情绪 Triste *
6楼-- · 2020-02-17 05:49

You may want to log at the highest level, which is usually your UI or web service code. Logging multiple times is sort of a waste. Also, you want to know the whole story when you are looking at the log.

In one of our applications, all of our pages are derived from a BasePage object, and this object handles the exception handling and error logging.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2020-02-17 05:49

It's good practice is to translate the exceptions. Don't just log them. If you want to know the specific reason an exception was thrown, throw specific exceptions:

public void connect() throws ConnectionException {
   try {
       File conf = new File("blabla");
       ...
   } catch (FileNotFoundException ex) {
       LOGGER.error("log message", ex);
       throw new ConnectionException("The configuration file was not found", ex);
   }
}
查看更多
登录 后发表回答