-
Notifications
You must be signed in to change notification settings - Fork 6k
Description
Type of issue
Missing information
Description
The article on CA2219: Do not raise exceptions in exception clauses currently (as of 2025-09-03) provides only a dogmatic "you should never do this" statement but does not suggest any alternative approaches to resolve the CA warning:
I quote (emphasis mine):
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.
Its easy to trigger CA2219 warnings when working with COM or PInvoke in C#, where our finally
or void Dispose
methods call into native methods that can/will/do fail with exceptions or runtime errors of their own; this includes C# code generated by CsWin32, for example.
Normally I'd provide some text to be copied+pasted into the article, but such-as-it-is, I don't know what the current Best Practices advice is for "failable-finally" clauses. I did find this related feedback suggesting passing exceptions around in locals - but it offends my aesthetics. In my own software I've stopped trying to faff-around with second-order exceptions and simply call Environment.FailFast
in event of an (unexpected) second-order exception, but I'm sure a better approach must exist.
Page URL
https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2219
Content source URL
https://github.com/dotnet/docs/blob/main/docs/fundamentals/code-analysis/quality-rules/ca2219.md
Document Version Independent Id
d07c8efd-99c8-3bde-5b4b-d762d9e77e8d
Platform Id
f5551239-15d6-1310-99f8-4573450bd87f
Article author
Metadata
- ID: a900d720-516e-d119-aba1-bb6db366774b
- PlatformId: f5551239-15d6-1310-99f8-4573450bd87f
- Service: dotnet-fundamentals