Record: 异常

在MSDN上有描述:ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_fxdesignguide/html/ab22ce03-78f9-4dca-8824-c7ed3bdccc27.htm

Exception 和 SystemException
不要引发 System.Exception 或 System.SystemException。
不要在框架代码中捕捉 System.Exception 或 System.SystemException,除非打算再次引发。
避免捕捉 System.Exception 或 System.SystemException,在顶级异常处理程序中除外。

ApplicationException
一定要从 T:System.Exception 类而不是 T:System.ApplicationException 类派生自定义异常。
最初考虑自定义异常应该从 ApplicationException 类派生;但是并未发现这样有很大意义。有关更多信息,请参见处理异常的最佳做法。

InvalidOperationException
如果处于不适当的状态,则引发 System.InvalidOperationException 异常。如果没有向属性集或方法调用提供适当的对象当前状态,则应引发System.InvalidOperationException。例如,向已打开用于读取的 System.IO.FileStream 写入时,应引发 System.InvalidOperationException 异常。一组相关对象的组合状态对于操作无效时,也应引发此异常。

ArgumentException、ArgumentNullException 和 ArgumentOutOfRangeException
如果向成员传递了错误的参数,则引发 System.ArgumentException 或其子类型之一。如果适用,首选派生程度最高的异常类型。
下面的代码示例演示当参数为 null(在 Visual Basic 中为 Nothing)时引发异常。

if  (anObject  ==   null )
ExpandedBlockStart.gifContractedBlock.gif
{
    
throw new ArgumentNullException("anObject""Specify a non-null argument.");
}

使用属性 setter 的隐式值参数的名称的值。
下面的代码示例演示一个属性,该属性在调用方传递 null 参数时引发异常。

ContractedBlock.gif ExpandedBlockStart.gif Code
public IPAddress Address
ExpandedBlockStart.gifContractedBlock.gif
{
    
get
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return address;
    }

    
set
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if(value == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
throw new ArgumentNullException("value");
        }

        address 
= value;
    }

}

不要允许可公开调用的 API 显式或隐式引发 System.NullReferenceException、System.AccessViolationException、System.InvalidCastException 或 System.IndexOutOfRangeException。进行参数检查以避免引发这些异常。引发这些异常会公开方法的实现细节,这些细节可能会随时间发生更改。

StackOverflowException
不要显式引发 System.StackOverflowException。此异常只应由公共语言运行库 (CLR) 显式引发。
不要捕捉 System.StackOverflowException。
以编程方式处理堆栈溢出极为困难。应允许此异常终止进程并使用调试确定问题的根源。

OutOfMemoryException
不要显式引发 System.OutOfMemoryException。此异常只应由 CLR 基础结构引发。

ComException 和 SEHException
不要显式引发 System.Runtime.InteropServices.COMException 或 System.Runtime.InteropServices.SEHException。这些异常只应由 CLR 基础结构引发。不要显式捕捉 System.Runtime.InteropServices.SEHException。

ExecutionEngineException
不要显式引发 System.ExecutionEngineException。

 
另外在CLR via C# 2nd一书中列出了.net 2.0的一些异常类型
System.Exception
System.ApplicationException
System.Reflection.InvalidFilterCriteriaException
System.Reflection.TargetException
System.Reflection.TargetInvocationException
System.Reflection.TargetParameterCountException
System.Threading.WaitHandleCannotBeOpenedException
System.IO.IsolatedStorage.IsolatedStorageException
System.Runtime.CompilerServices.RuntimeWrappedException
System.SystemException
System.AccessViolationException
System.AppDomainUnloadedException
System.ArgumentException
System.ArgumentNullException
System.ArgumentOutOfRangeException
System.DuplicateWaitObjectException
System.Text.DecoderFallbackException
System.Text.EncoderFallbackException
System.ArithmeticException
System.DivideByZeroException
System.NotFiniteNumberException
System.OverflowException
System.ArrayTypeMismatchException
System.BadImageFormatException
System.CannotUnloadAppDomainException
System.Collections.Generic.KeyNotFoundException
System.ContextMarshalException
System.DataMisalignedException
System.ExecutionEngineException
System.FormatException
System.Reflection.CustomAttributeFormatException
System.IndexOutOfRangeException
System.InvalidCastException
System.InvalidOperationException
System.ObjectDisposedException
System.InvalidProgramException
System.IO.IOException
System.IO.DirectoryNotFoundException
System.IO.DriveNotFoundException
System.IO.EndOfStreamException
System.IO.FileLoadException
System.IO.FileNotFoundException
System.IO.PathTooLongException
System.MemberAccessException
System.FieldAccessException
System.MethodAccessException
System.MissingMemberException
System.MissingFieldException
System.MissingMethodException
System.MulticastNotSupportedException
System.NotImplementedException
System.NotSupportedException
System.PlatformNotSupportedException
System.NullReferenceException
System.OperationCanceledException
System.OutOfMemoryException
System.InsufficientMemoryException
System.RankException
System.Reflection.AmbiguousMatchException
System.Reflection.ReflectionTypeLoadException
System.Resources.MissingManifestResourceException
System.Resources.MissingSatelliteAssemblyException
System.Runtime.InteropServices.ExternalException
System.Runtime.InteropServices.COMException
System.Runtime.InteropServices.SEHException
System.Runtime.InteropServices.InvalidComObjectException
System.Runtime.InteropServices.InvalidOleVariantTypeException
System.Runtime.InteropServices.MarshalDirectiveException
System.Runtime.InteropServices.SafeArrayRankMismatchException
System.Runtime.InteropServices.SafeArrayTypeMismatchException
System.Runtime.Remoting.RemotingException
System.Runtime.Remoting.RemotingTimeoutException
System.Runtime.Remoting.ServerException
System.Runtime.Serialization.SerializationException
System.Security.Cryptography.CryptographicException
System.Security.Cryptography.CryptographicUnexpectedOperationException
System.Security.HostProtectionException
System.Security.Policy.PolicyException
System.Security.Principal.IdentityNotMappedException
System.Security.SecurityException
System.Security.VerificationException
System.Security.XmlSyntaxException
System.StackOverflowException
System.Threading.AbandonedMutexException
System.Threading.SynchronizationLockException
System.Threading.ThreadAbortException
System.Threading.ThreadInterruptedException
System.Threading.ThreadStartException
System.Threading.ThreadStateException
System.TimeoutException
System.TypeInitializationException
System.TypeLoadException
System.DllNotFoundException
System.EntryPointNotFoundException
System.TypeUnloadedException
System.UnauthorizedAccessException
System.Security.Accesscontrol.PrivilegeNotHeldException

另外在来自微软关于异常处理的17条军规一文中也描述了一些实用异常的规则。

转载于:https://www.cnblogs.com/cwblaze/archive/2009/09/17/1568299.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值