title | description | ms.date | f1_keywords | helpviewer_keywords | author | ms.author | dev_langs | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
CA1031: Do not catch general exception types (code analysis) |
Learn about code analysis rule CA1031: Do not catch general exception types |
11/04/2016 |
|
|
gewarren |
gewarren |
|
Property | Value |
---|---|
Rule ID | CA1031 |
Title | Do not catch general exception types |
Category | Design |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 8 | No |
A general exception such as xref:System.Exception?displayProperty=fullName or xref:System.SystemException?displayProperty=fullName is caught in a catch
statement, or a general catch clause such as catch()
is used.
By default, this rule only flags general exception types being caught, but this is configurable.
General exceptions should not be caught.
To fix a violation of this rule, catch a more specific exception, or rethrow the general exception as the last statement in the catch
block.
Do not suppress a warning from this rule. Catching general exception types can hide run-time problems from the library user and can make debugging more difficult.
Note
Starting with .NET Framework 4, the common language runtime (CLR) no longer delivers corrupted state exceptions that occur in the operating system and managed code, such as access violations in Windows, to be handled by managed code. If you want to compile an application in .NET Framework 4 or later versions and maintain handling of corrupted state exceptions, you can apply the xref:System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute attribute to the method that handles the corrupted state exception.
Use the following option to configure which parts of your codebase to run this rule on.
You can configure this option for just this rule, for all rules it applies to, or for all rules in this category (Design) that it applies to. For more information, see Code quality rule configuration options.
You can configure which exception types are disallowed from being caught. For example, to specify that the rule should flag catch
handlers with NullReferenceException
, add the following key-value pair to an .editorconfig file in your project:
dotnet_code_quality.CA1031.disallowed_symbol_names = NullReferenceException
Allowed type name formats in the option value (separated by |
):
- Type name only (includes all symbols with the name, regardless of the containing type or namespace)
- Fully qualified names in the symbol's documentation ID format with a
T:
prefix.
Examples:
Option value | Summary |
---|---|
dotnet_code_quality.CA1031.disallowed_symbol_names = ExceptionType |
Matches all symbols named 'ExceptionType' in the compilation. |
`dotnet_code_quality.CA1031.disallowed_symbol_names = ExceptionType1 | ExceptionType2` |
dotnet_code_quality.CA1031.disallowed_symbol_names = T:NS.ExceptionType |
Matches specific types named 'ExceptionType' with given fully qualified name. |
`dotnet_code_quality.CA1031.disallowed_symbol_names = T:NS1.ExceptionType1 | T:NS1.ExceptionType2` |
You can configure these options for just this rule, for all rules it applies to, or for all rules in this category (Design) that it applies to. For more information, see Code quality rule configuration options.
The following example shows a type that violates this rule and a type that correctly implements the catch
block.
:::code language="vb" source="snippets/vb/all-rules/ca1031-do-not-catch-general-exception-types_1.vb":::
:::code language="csharp" source="snippets/csharp/all-rules/ca1031.cs" id="snippet1":::