Share via


CA2219: Do not raise exceptions in exception clauses

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName DoNotRaiseExceptionsInExceptionClauses
CheckId CA2219
Category Microsoft.Usage
Breaking Change Non Breaking, Breaking

Cause

An exception is thrown from a finally, filter, or fault clause.

Rule Description

When an exception is raised in an exception clause, it greatly increases the difficulty of debugging.

When an exception is raised in a finally or fault clause, the new exception hides the active exception, if present. This makes the original error hard to detect and debug.

When an exception is raised in a filter clause, the runtime silently catches the exception, and causes the filter to evaluate to false. There is no way to tell the difference between the filter evaluating to false and an exception being throw from a filter. This makes it hard to detect and debug errors in the filter's logic.

How to Fix Violations

To fix this violation of this rule, do not explicitly raise an exception from a finally, filter, or fault clause.

When to Suppress Warnings

Do not suppress a warning for this rule. There are no scenarios under which an exception raised in an exception clause provides a benefit to the executing code.

CA1065: Do not raise exceptions in unexpected locations

See Also

Design Warnings