C# catch(FileNotFoundException) and CA1031

孤人 提交于 2020-06-27 07:28:34

问题


So this code triggers CA1031.

try
{
    // logic
}
catch (FileNotFoundException) // exception type
{
    // handle error
}

While this one does not:

try
{
    // logic
}
catch (FileNotFoundException ex) // exception var
{
    // handle error
}

Because the exception type is meaningful, I don't need the ex in the first example. But it's not a a general exception type. It's not IOException or Exception. So why does it still trigger the CA1031?

So is there a difference between catch(FileNotFoundException) and catch(FileNotFoundException ex) outside the fact that I don't capture exception info?


回答1:


So this code triggers CA1031

try
{
    // logic
}
catch (FileNotFoundException) // exception type
{
    // handle error
}

This occurs because a "general exception such as System.Exception or System.SystemException is caught in a catch statement, or a general catch clause such as catch() is used". To fix it, assign it and handle the error and or rethrow the general exception for it to be handled further up.

Upon further investigation, it seems this used to be an bug, you can see more here; it was a Roslyn issue for FxCop.

To Fix: Just update the latest FxCop analyzers package and it should go way.

NuGet:

 Install-Package Microsoft.CodeAnalysis.FxCopAnalyzers -Version 2.9.7

References: CA1031




回答2:


I have two Articles I use as basis for my Exception handling:

  • https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/
  • https://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET

I also link those often when I notice Exception handling errors.

FileNotFound is clearly a exogenous Exception, so it is correct to catch it. However those articles also tell that as a general rule, to always log or expose those Exceptions. Ideally the result of Exception.ToString(). If you do not have a way to reference the caught exception, how could you do either of those two? You can only give a generic error message, but with none of the details you will actually need to debug it.

While there are many cases where you only want to expose the Exception type to the user, there is never one where you only want to log the Exception type. The linked articles mention that explicitly, but due to downvotes and comments it seems nessesary for me to repeat that.

So it is one of those cases where the argument is still going if it is a bug or a feature.

For me it certainly feels more like a feature. I would certainly call you out as potentiall issue, if I saw it in your code. It avoids you under-logging stuff. You could test if the error persist if you write throw; at the end of the catch block. This will re-throw on the exception, so a lack of being able to reference the exception in this ExceptionHandler would not be critical.



来源:https://stackoverflow.com/questions/58648645/c-sharp-catchfilenotfoundexception-and-ca1031

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!