Do not raise reserved exception types
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.
- 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.
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>
Open in the interactive Rules Explorer · All default rules
.NET Framework Usage / System: all 12 items
- Mark ISerializable types with SerializableAttribute ND2200
- Mark assemblies with CLSCompliant (deprecated) ND2201
- Mark assemblies with ComVisible (deprecated) ND2202
- Mark attributes with AttributeUsageAttribute ND2203
- Remove calls to GC.Collect() ND2204
- Don't call GC.Collect() without calling GC.WaitForPendingFinalizers() ND2205
- Enum Storage should be Int32 ND2206
- Do not raise too general exception types ND2207
- Do not raise reserved exception types ND2208
- Uri fields or properties should be of type System.Uri ND2209
- Types should not derive from System.ApplicationException ND2210
- Don't Implement ICloneable ND2211