All rules
CA1031Design Enabled by default: No MS default: Suggestion

Do not catch general exception types

Avoid catching `Exception` or `SystemException` broadly.

Microsoft docs

Description

Catching general exception types can swallow unexpected errors and hide bugs. Catch specific exception types instead.

Cause

A general exception such as System.Exception or System.SystemException 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.

Why it matters

Broad catches mask programming errors and make failures harder to diagnose.

How to fix violations

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.

Examples

Avoid
try { Run(); }
catch (Exception) { }
Prefer
try { Run(); }
catch (IOException ex) { Log(ex); }

When to suppress

Do not suppress a warning from this rule. Catching general exception types can hide runtime problems from the library user and can make debugging more difficult. 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 System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute attribute to the method that handles the corrupted state exception.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0