This section describes the standard exceptions provided by the Framework and the details of their usage. The list is by no means exhaustive. Please refer to the .NET Framework reference documentation for usage of other Framework exception types.
X DO NOT throw System.Exception or System.SystemException.
X DO NOT catch System.Exception
or System.SystemException
in framework code, unless you intend to rethrow.
X AVOID catching System.Exception
or System.SystemException
, except in top-level exception handlers.
X DO NOT throw or derive from ApplicationException.
✓ DO throw an InvalidOperationException if the object is in an inappropriate state.
✓ DO throw ArgumentException or one of its subtypes if bad arguments are passed to a member. Prefer the most derived exception type, if applicable.
✓ DO set the ParamName
property when throwing one of the subclasses of ArgumentException
.
This property represents the name of the parameter that caused the exception to be thrown. Note that the property can be set using one of the constructor overloads.
✓ DO use value
for the name of the implicit value parameter of property setters.
X DO NOT allow publicly callable APIs to explicitly or implicitly throw NullReferenceException, AccessViolationException, orIndexOutOfRangeException. These exceptions are reserved and thrown by the execution engine and in most cases indicate a bug.
Do argument checking to avoid throwing these exceptions. Throwing these exceptions exposes implementation details of your method that might change over time.
X DO NOT explicitly throw StackOverflowException. The exception should be explicitly thrown only by the CLR.
X DO NOT catch StackOverflowException
.
It is almost impossible to write managed code that remains consistent in the presence of arbitrary stack overflows. The unmanaged parts of the CLR remain consistent by using probes to move stack overflows to well-defined places rather than by backing out from arbitrary stack overflows.
X DO NOT explicitly throw OutOfMemoryException. This exception is to be thrown only by the CLR infrastructure.
X DO NOT explicitly throw COMException, ExecutionEngineException, and SEHException. These exceptions are to be thrown only by the CLR infrastructure.
Portions © 2005, 2009 Microsoft Corporation. All rights reserved.
Reprinted by permission of Pearson Education, Inc. from Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, 2nd Edition by Krzysztof Cwalina and Brad Abrams, published Oct 22, 2008 by Addison-Wesley Professional as part of the Microsoft Windows Development Series.
출처 : https://msdn.microsoft.com/en-us/library/ms229007(v=vs.110).aspx
'Language > C#' 카테고리의 다른 글
Nested Type 설계 시 주의할 점 (0) | 2017.07.27 |
---|---|
Old csproj to new csproj: Visual Studio 2017 upgrade guide (0) | 2017.05.22 |
C# Singleton Pattern (0) | 2017.03.08 |
[C#] 문자열이 숫자인지 확인하는 방법 (0) | 2013.12.24 |
[C#] 개체 참조가 개체의 인스턴스로 설정되지 않았습니다. [출처] C# : 오류 System.NullReferenceException: 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.(오류 System.NullReferenceException) (0) | 2013.12.14 |