Do not raise reserved exception types

Rule ND2208 .NET Framework Usage / System

These reserved exception types should be thrown only by the runtime:

- System.ExecutionEngineException

- System.IndexOutOfRangeException

- System.OutOfMemoryException

- System.StackOverflowException

- System.InvalidProgramException

- System.AccessViolationException

- System.CannotUnloadAppDomainException

- System.BadImageFormatException

- System.DataMisalignedException

Matches methods instantiating such reserved exceptions.

How to fix

Throw a specific non-reserved exception type instead.

For System.NullReferenceException, throw System.ArgumentNullException instead, or use a contract (MS Code Contracts or Debug.Assert()) meaning a null there can only be a bug. More generally, on detected corrupted state / bug consequence, a declarative assertion beats throwing: continuing is potentially harmful, the process should shut down and the failure be reported as a bug.

CQLinq source

This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

// <Name>Do not raise reserved exception types</Name> 
// <Id>ND2208:DoNotRaiseReservedExceptionTypes</Id>

warnif count > 0 

let reservedExceptions = ThirdParty.Types.WithFullNameIn(   
   "System.ExecutionEngineException",
   "System.IndexOutOfRangeException",
   "System.OutOfMemoryException",
   "System.StackOverflowException",
   "System.InvalidProgramException",
   "System.AccessViolationException",
   "System.CannotUnloadAppDomainException",
   "System.BadImageFormatException",
   "System.DataMisalignedException")

from m in JustMyCode.Methods.ThatCreateAny(reservedExceptions)
where !m.IsGeneratedByCompiler
let reservedExceptionsCreated = reservedExceptions.Where(t => m.IsUsing(t))
select new { 
   m,
   reservedExceptionsCreated,
   Debt = 10.ToMinutes().ToDebt(),
   Severity = Severity.High
}

//<Expl>
// The *{0}* creates an instance of the *{1}*. This is a reserved exception 
// that should be thrown only by the runtime.
//</Expl>

//<Desc>
// These reserved exception types should be thrown only by the runtime:
//
// - System.ExecutionEngineException
//
// - System.IndexOutOfRangeException
//
// - System.OutOfMemoryException
//
// - System.StackOverflowException
//
// - System.InvalidProgramException
//
// - System.AccessViolationException
//
// - System.CannotUnloadAppDomainException
//
// - System.BadImageFormatException
//
// - System.DataMisalignedException
//
// Matches methods instantiating such reserved exceptions.
//</Desc>

//<HowToFix>
// Throw a specific non-reserved exception type instead.
//
// For *System.NullReferenceException*, throw
// *System.ArgumentNullException* instead, or use a contract (MS Code
// Contracts or *Debug.Assert()*) meaning a null there can only be a bug.
// More generally, on detected *corrupted state / bug consequence*, a
// declarative assertion beats throwing: continuing is potentially harmful,
// the process should shut down and the failure be reported as a bug.
//</HowToFix>

.NET Framework Usage / System: all 12 items