评论try catch语句

时间:2009-01-05 18:10:30

标签: c# error-handling comments

在try-catch语句中解释错误处理的适当位置是什么?看起来您可以将解释性注释放在try块的开头或catch块中。

// Possible comment location 1
try
{   
    // real code
}
// Possible comment location 2
catch
{
    // Possible comment location 3

    // Error handling code

}

8 个答案:

答案 0 :(得分:17)

我通常会做以下事情。如果只处理一个例外,我通常不会打扰,因为它应该是自我记录的。

try
{   
    real code // throws SomeException
    real code // throws SomeOtherException
}
catch(SomeException se)
{
    // explain your error handling choice if it's not obvious
}
catch(SomeOtherException soe)
{
    // explain your error handling choice if it's not obvious
}

答案 1 :(得分:10)

“评论是谎言”。处理这些变量名称和一般逻辑,以便您可以避免它。如果你真的需要撒谎,可以在catch区内进行。

答案 2 :(得分:5)

我觉得这根本不重要。

我认为通过评论记住的重要事项是解决为什么代码是这样的,而不是代码正在做什么,首先是。这并不是说你不应该在简明的评论中解释复杂的逻辑,但为什么这么重要。

答案 3 :(得分:4)

如何设置代码以便不需要额外的注释?

try
{ 
   performDifficultAct( parameter );
}
catch (ArgumentOutOfRangeException couldNotFindArgument)
{
   // handle exception
}
catch (Exception otherUnknownException )
{
   // handle exception
}

无需记录您是否可以使用变量和方法命名来显示正在发生的事情。无需记录是否必须记录或引发异常 - 源代码中的记录消息无论如何都应该是不言自明的。唯一一次你需要在你的代码中需要额外的文档时,代码正在做什么是完全不明显的,或者你需要添加一个容易错过的问题或模糊步骤,这对于任何看到它的人都需要解释代码将来。

编辑:为了澄清一点,这里有更多关于如何使用这些“catch”语句向维护程序员和用户/支持/ QA /使用该软件的任何其他人提供有用信息的方法。还说明了我绝对想在代码中添加额外注释的情况:

public void PerformSomeActionOrOther(string parameter)
{
  try
  { 
     // For some reason an eleven character string causes a bluescreen from Kernel32
     if (parameter.Length==11) parameter+=" ";

     performDifficultAct( parameter );
  }
  catch (ArgumentOutOfRangeException couldNotFindArgument)
  {
     this.Log.WriteLn("Argument out of range exception in ArbitraryClass.PerformSomeActionOrOther");
     this.Log.WriteLn(String.Format("Probable cause is that {0} is not in the array", parameter));
     this.Log.WriteLn(String.Format("Exception: {0}", couldNotFindArgument.Message));
  }
  catch (Exception otherUnknownException )
  {
     this.Log.WriteLn("Unexpected exception in ArbitraryClass.PerformSomeActionOrOther");
     this.Log.WriteLn(String.Format("Exception: {0}", otherUnknownException.Message));
     throw( otherUnknownException );
  }
}

答案 4 :(得分:2)

绝对不要对它的顶部进行评论,因为除了“在这里启动异常处理块”之外,你能说些什么呢?关于捕获声明的评论更好,但总的来说,你还会说些什么? “处理NullPointerException”?

我会去评论IFF你需要说你正在做一些令人兴奋的事情,比如链接到应用程序域异常。

答案 5 :(得分:1)

我认为一个写得很好的try / catch应该简明扼要。我同意@Jason的说法为什么更为重要,但同样重要的是要尽可能简洁地将代码保持在内部。

如果您使用了特定的异常,它也会有所帮助。例如,如果您使用Java,请尝试捕获NullPointerException而不是通用Exception。这可以解释为什么try catch存在以及你正在做什么来解决它。

答案 6 :(得分:1)

只要您保持一致,位置无关紧要。我个人的偏好如下:

//comment 1: code does XYZ, can cause exceptions A, B, C
try {
    //do something
}
//comment 2: exception A occurs when foo != bar
catch (ExceptionA a) {
    //do something
}
//comment 3: exception B occurs when bar is null
catch (ExceptionB b) {
    //do something
}
//comment 4: exception B occurs when foo is null
catch (ExceptionC c) {
    //do something
}

答案 7 :(得分:0)

我知道这不是您正在寻找的答案,但根本不予评论。如果您的代码不够清晰,无需评论就可以自行站立,那么您应该重构它直到它为止。 Jeffrey Palerm o刚刚写了blog post,表明最好。

通常,评论倾向于记录:

  • 代码太紧凑了。看起来像这样的事情:++i?--g:h-i;
  • 需要汇总的长代码块
  • 代码既可以是一次性的,也可以没有明确的原因

有关异常块的一些简单注释的简化示例,以及不需要注释的版本,请参见下文。

bool retries = 0;
while (retries < MAX_RETRIES)
{
    try
    {
        ... database access code
        break;
    }
    // If under max retries, log and increment, otherwise rethrow
    catch (SqlException e)
    {
        logger.LogWarning(e);
        if (++retries >= MAX_RETRIES)
        {
            throw new MaxRetriesException(MAX_RETRIES, e);
        }
    }
    // Can't retry.  Log error and rethrow.
    catch (ApplicationException e)
    {
        logger.LogError(e);
        throw;
    }
}

虽然上述注释提升了可重用性,但您基本上必须同时维护代码和注释。有可能(并且更可取)重构这一点,以便在没有评论的情况下更清晰。

bool retries = 0;
while (canRetry(retries))
{
    try
    {
        ... database access code
        break;
    }
    catch (SqlException e)
    {
        logger.LogWarning(e);
        retries = incrementRetriesOrThrowIfMaxReached(retries, e);
    }
    catch (ApplicationException e)
    {
        logger.LogError(e);
        throw;
    }
}

...

private void incrementRetriesOrThrowIfMaxReached(int retries, Exception e)
{
    if (++retries >= MAX_RETRIES)
        throw new MaxRetriesException(MAX_RETRIES, e);

    return retries;
}

private bool canRetry(int retries)
{
    return retries < MAX_RETRIES;
}

后一个例子可能看起来像是一个非常微妙的好处的代码,但收益不容小觑。代码也是可以理解的,但是你有一个好处,就是你不需要有一组单独的元数据(注释)来解释代码。代码解释了自己。如果您的catch代码块太长并需要注释进行总结,请考虑将其重构为单独的方法以提高可读性。