简体   繁体   中英

Change EditorConfig to flag certain exceptions

Is there a way to cause using a certain type of Exception to show as an error (or warning, or info) for .NET projects in Visual Studio?

I was thinking changing the .editorconfig file would do the trick, but even with the options available for code analysis I can't find a way to do this.

try
{
    //your code that sometimes throws exceptions
}
catch(Exception e)
{
     if(e is NullReferenceException)
     {
         Error();
     } else
     Warning();
}

This is not a task for editorConfig alone, but requires StyleCop to be enabled as well. Rule CA1031 is designed for this task.

You can set in your .editorConfig :

dotnet_code_quality.CA1031.disallowed_symbol_names = NullReferenceException

(If you don't want that people catch NullReferenceException anywhere)

StyleCop is enabled on .NET 5.0 projects by default, for older projects, it must be enabled by including the appropriate nuget package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM