You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CC0004 analyzer unable to handle filters. I some cases I need to log exceptions (catch block never reached). I can disable warning by adding #pragma warning disable, but I would like to avoid that. Can I expect that to be fixed in coming releases?
Before:
staticvoidMain(){try{// Some code...}catch(Exceptionex)when(Log(ex)){}}staticboolLog(Exceptionex){
Console.WriteLine(ex.Message);returnfalse;}
After:
try{// Some code...}
#pragma warning disable CC0004 // Catch block cannot be emptycatch(Exceptionex)when(Log(ex)){}
#pragma warning restore CC0004 // Catch block cannot be empty}staticboolLog(Exceptionex){
Console.WriteLine(ex.Message);returnfalse;}
The text was updated successfully, but these errors were encountered:
At first glance I was going to recommend against this as a typical monadic comprehension (like filter/where) would not guard against side effects. However, checking the C# 6 FAQ, this behavior is actually recommended:
It is also a common and accepted form of “abuse” to use exception filters for side effects; e.g. logging. They can inspect an exception “flying by” without intercepting its course. In those cases, the filter will often be a call to a false-returning helper function which executes the side effects:
This may be a valid extension to make for this analyzer. We could go even further and have an analyzer that detects the following pattern and recommends using an expression filter to avoid the catch/throw performance penalty:
#New analyzer:
CC0004 analyzer unable to handle filters. I some cases I need to log exceptions (catch block never reached). I can disable warning by adding #pragma warning disable, but I would like to avoid that. Can I expect that to be fixed in coming releases?
Before:
After:
The text was updated successfully, but these errors were encountered: