Open sandboxFocus

Method CreateException

CreateException(ContractExceptionInfo)

Base class for contract exception factories. Implements the chain of responsibility between concrete exception factories.

Declaration
public override Exception CreateException(ContractExceptionInfo exceptionInfo)
Parameters
Type Name Description
ContractExceptionInfo exceptionInfo
Returns
Type Description
Exception
Overrides
Remarks

To change the exceptions that contracts throw, derive a class from ContractExceptionFactory and override the CreateException(ContractExceptionInfo) method. Return an exception for the cases that your factory handles, and call the base implementation for the others, which invokes the next factory in the chain. At application startup, assign an instance of your factory to ExceptionFactory, and pass the previous value of this property to the constructor.

The default factory creates ArgumentException, ArgumentNullException, ArgumentOutOfRangeException and PostconditionFailedException, with a message obtained from LocalizedTextProvider. When no factory of the chain handles the requested exception type, the base implementation throws an InvalidOperationException.

Custom contracts can create their exceptions through ExceptionFactory, so that the same customizations apply to them.

See Also