Exceptions should be public

TypeName

ExceptionsShouldBePublic

CheckId

CA1064

Category

Microsoft.Design

Breaking Change

Non Breaking

Cause

A non-public exception derives directly from System.Exception, System.SystemException, or System.ApplicationException.

Rule Description

An internal exception is only visible inside its own internal scope. After the exception falls outside the internal scope, only the base exception can be used to catch the exception. If the internal exception is inherited from T:System.Exception, T:System.SystemException, or T:System.ApplicationException, the external code will not have sufficient information to know what to do with the exception.

But, if the code has a public exception that later is used as the base for a internal exception, it is reasonable to assume the code further out will be able to do something intelligent with the base exception. The public exception will have more information than what is provided by T:System.Exception, T:System.SystemException, or T:System.ApplicationException.

How to Fix Violations

Make the exception public, or derive the internal exception from a public exception that is not System.Exception, System.SystemException, or System.ApplicationException.

When to Suppress Warnings

Suppress a message from this rule if you are certain in all cases that the private exception will be caught within its own internal scope.

Example

The following shows three examples which violate the rule.