Skip to main content

RCS1236: Use exception filter

Properties

PropertyValue
Default SeverityInfo
Minimum language version6.0

Examples

Example #1

diagnostic.cs
try
{
}
catch (Exception ex)
{
if (!(ex is InvalidOperationException))
{
throw;
}

return;
}
fix.cs
try
{
}
catch (Exception ex) when (ex is InvalidOperationException)
{
return;
}

Remarks

The accepted answer from Stack Overflow states:

"If there is an exception thrown within the filter, then that exception will be silently swallowed and the filter simply fails."

It is impossible to definitely detect if an exception can be thrown within an expression.

So the expression is considered as the one that can throw an exception if it contains method call that meets one of the following requirements:

  • Its name starts with ThrowIf.
  • Its XML comment contains <exception> element.

Applies to