Eri*_*ess 4 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
}
Run Code Online (Sandbox Code Playgroud)
Bil*_*ard 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
}
Run Code Online (Sandbox Code Playgroud)
我认为这根本不重要.
我想进口的东西与评论要记住的是,解决为什么代码是现在这个样子,而不是什么代码是干什么的,第一位的.这并不是说你不应该在简明的评论中解释复杂的逻辑,但为什么这么重要.