我正在通过FxCop运行一些代码,并且目前正在清除所有非强制性违规。
代码本身有一些try/catch块,只是捕获常规异常;
try
{
// Some code in here that could throw an exception
}
catch(Exception ex)
{
// Exception Thrown ... sort it out!
}
现在我们都知道这是不好的实践,但我认为我知道如何正确地做到这一点,但 FxCop 有其他想法!
假设 try 块中的代码可能会抛出一个 IO 异常,而且仅限于 IO 异常。像这样做应该没有任何问题:
try
{
// Code in here that can only throw an IOException
}
catch (System.IO.IOException ioExp)
{
// Handle the IO exception or throw it
}
catch (System.Exception ex)
{
// Catch otherwise unhandled exception
}
但是FxCop不同意我的观点...它仍然将此标记为违规,因为我捕获了System.Exception。这真的是不好的实践吗?还是说我可以安全地忽略这个违规?