1

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.

Aaron Thomas
  • 5,054
  • 8
  • 43
  • 89
  • Exceptions are unexpected States. If you expect a specfic state, check for it. If it isn't true, show a warning or an Info. Use exceptions only if something unexpected is happen. – Christian Gollhardt Oct 20 '21 at 22:32
  • @ChristianGollhardt the question is concerning when exceptions are present in the code, such as when the words `throw new NotImplementedException();` are literally in a .cs file. This has nothing to do with runtime state. – Aaron Thomas Oct 22 '21 at 01:05
  • Well, if it's a custom exception, you could trigger a warning by using the `ObsoleteAttribute` , an Error by commenting it out. That being said, I think you are looking for an [RoslynAnalyzer](https://joshvarty.com/learn-roslyn-now/). – Christian Gollhardt Oct 22 '21 at 10:18

1 Answers1

0

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.

PMF
  • 14,535
  • 3
  • 23
  • 49