Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions docs/fundamentals/code-analysis/quality-rules/ca2219.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "CA2219: Do not raise exceptions in exception clauses (code analysis)"
description: "Learn about code analysis rule CA2219: Do not raise exceptions in exception clauses"
ms.date: 11/04/2016
description: "Learn about code analysis rule CA2219: Do not raise exceptions in exception clauses, and explore alternative patterns for handling cleanup failures."
ms.date: 11/01/2026
f1_keywords:
- DoNotRaiseExceptionsInExceptionClauses
- CA2219
Expand Down Expand Up @@ -32,16 +32,50 @@ An exception is thrown from a `finally`, filter, or fault clause.

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 `finally` or fault clause, the new exception hides the active exception, if present. This masking 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.
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 thrown from a filter. This behavior 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.
To fix a violation of this rule, ensure that exceptions are not thrown from a `finally`, filter, or fault clause. Use the following patterns to handle operations that might fail during cleanup:

### Catch and log cleanup exceptions inside `finally`

If cleanup operations—such as disposing unmanaged resources, closing streams, or calling native P/Invoke APIs—can fail, wrap those operations in an internal `try/catch` block inside the `finally` clause:

```csharp
try
Comment on lines +47 to +48
{
PerformPrimaryOperation();
}
finally
{
try
{
CleanupResource();
}
catch (Exception ex)
{
// Log the cleanup exception without rethrowing,
// preventing it from masking any in-flight exception.
logger.LogError(ex, "Failed to clean up resource.");
}
}
```

### Avoid throwing inside exception filters

Exception filters (`catch (...) when (condition)`) should only execute non-throwing expressions. If an expression inside a filter throws an exception, the runtime catches it silently and evaluates the filter as `false`. To log exceptions during filtering, call methods that handle their own internal errors and safely return `false`.

### Use `Environment.FailFast` for unrecoverable cleanup failures

If a resource failure in a `finally` block indicates critical corruption where the application cannot safely continue running (for example, critical state corruption in native interop), call <xref:System.Environment.FailFast*?displayProperty=nameWithType> instead of throwing a managed exception. `FailFast` terminates the process immediately and generates a crash report without executing further finalizers or handlers.

## Example

The following example shows code that violates the rule:

```csharp
try
{
Expand All @@ -56,7 +90,7 @@ finally

## 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.
Do not suppress a warning for this rule. An unhandled exception escaping from a `finally` block discards and overrides any active exception from the `try` block, obscuring the root cause of failures.

## Related rules

Expand Down