如何使用try捕获的异常处理的最佳实践如何使用、异常、try

2023-09-02 21:08:41 作者:温柔从犯

在保持我的同事的code从甚至有人声称自己是谁的高级开发人员,我经常看到下面的code:

while maintaining my colleague's code from even someone who claims to be a senior developer, I often see the following code:

try
{
  //do something
}
catch
{
  //Do nothing
}

有时他们写日志信息到日志文件中像下面试捕

try
{
  //do some work
}
catch(Exception exception)
{
   WriteException2LogFile(exception);
}

我只是想知道,如果他们做了什么是最好的做法是什么?这让我困惑,因为在我的思想的用户应该知道,该系统会发生什么。

I am just wondering if what they have done is the best practice? It makes me confused because in my thinking users should know what happens with the system.

请给我一些建议。

推荐答案

我的异常处理策略是:

要通过挂钩到 Application.ThreadException事件搭上所有未处理的异常 ,然后决定:

To catch all unhandled exceptions by hooking to the Application.ThreadException event, then decide : 对于UI应用程序:将其弹出与道歉消息的用户(的WinForms) 对于一个服务或一个控制台应用程序:它记录到一个文件(服务或控制台)

然后我总是附上每件code时运行外部在的try / catch

通过的WinForms基础设施发射的所有事件(加载,点击,SelectedChanged ...) 在由第三方组件所激发的所有事件

然后我请用'的try / catch

Then I enclose in 'try/catch'

所有的操作我的知道可能无法工作的所有时间的(IO操作,计算与潜在的零除...)。在这种情况下,我抛出一个新的 ApplicationException的(自定义消息的InnerException)来跟踪到底发生了什么 All the operations that I know might not work all the time (IO operations, calculations with a potential zero division...). In such a case, I throw a new ApplicationException("custom message", innerException) to keep track of what really happened

此外,我尽我所能为某种异常正确。当然也有例外,其中:

Additionally, I try my best to sort exceptions correctly. There are exceptions which:

需要被显示给用户立即 需要一些额外的处理,把东西放在一起时,他们碰巧避免级联问题(即:在一个把.EndUpdate在最后部分的TreeView 填充)

用户并不关心,但重要的是要知道发生了什么。所以,我总是记录它们: need to be shown to the user immediately require some extra processing to put things together when they happen to avoid cascading problems (ie: put .EndUpdate in the finally section during a TreeView fill) 如何优雅的实现 try catch 异常块

the user does not care, but it is important to know what happened. So I always log them: 在事件日志中 或磁盘上的.log文件

这是一个很好的做法为设计的一些静态方法来处理异常应用程序中的顶级错误处理程序。

It is a good practice to design some static methods to handle exceptions in the application top level error handlers.

我也强迫自己去尝试:

记住所有异常被冒泡到顶级。这是没有必要把异常处理处处。 在可重复使用或深层调用的函数并不需要显示或记录异常:他们要么自动向上冒泡或重新抛出,在我的异常处理一些自定义的消息。 Remember ALL exceptions are bubbled up to the top level. It is not necessary to put exception handlers everywhere. Reusable or deep called functions does not need to display or log exceptions : they are either bubbled up automatically or rethrown with some custom messages in my exception handlers.

所以最后:

为:

// DON'T DO THIS, ITS BAD
try
{
    ...
}
catch 
{
   // only air...
}

无用的:

// DONT'T DO THIS, ITS USELESS
try
{
    ...
}
catch(Exception ex)
{
    throw ex;
}

有一个尝试最终没有抓住是完全有效的:

Having a try finally without a catch is perfectly valid:

try
{
    listView1.BeginUpdate();

    // If an exception occurs in the following code, then the finally will be executed
    // and the exception will be thrown
    ...
}
finally
{
    // I WANT THIS CODE TO RUN EVENTUALLY REGARDLESS AN EXCEPTION OCCURED OR NOT
    listView1.EndUpdate();
}

我做的顶层:

What I do at the top level:

// i.e When the user clicks on a button
try
{
    ...
}
catch(Exception ex)
{
    ex.Log(ex); // Log exception
    // ex.LogAndDisplay(ex); // Log exception, then show it to the user with apologies...
}

我在做一些所谓的功能:

What I do in some called functions:

// Calculation module
try
{
    ...
}
catch(Exception ex)
{
    // Add useful information to the exception
    throw new ApplicationException("Something wrong happened in the calculation module :", ex);
}

// IO module
try
{
    ...
}
catch(Exception ex)
{
    throw new ApplicationException(string.Format("I cannot write the file {0} to {1}", fileName, directoryName), ex);
}

还有很多工作要做,与异常处理(自定义例外),但放入系统规则,我试图记住足以使简单的应用我做的。

There is a lot to do with exception handling (Custom Exceptions) but thoses rules I try to keep in mind are enough for the simple applications I do.

 
精彩推荐
图片推荐