Which built-in .NET exceptions can I throw from my application?

后端 未结 5 893
难免孤独
难免孤独 2021-01-30 08:04

If I need to throw an exception from within my application which of the built-in .NET exception classes can I use? Are they all fair game? When should I derive my own?

5条回答
  •  猫巷女王i
    2021-01-30 08:55

    See Creating and Throwing Exceptions.

    On throwing built-in exceptions, it says:

    Do not throw System.Exception, System.SystemException, System.NullReferenceException, or System.IndexOutOfRangeException intentionally from your own source code.

    and

    Do Not Throw General Exceptions

    If you throw a general exception type, such as Exception or SystemException in a library or framework, it forces consumers to catch all exceptions, including unknown exceptions that they do not know how to handle.

    Instead, either throw a more derived type that already exists in the framework, or create your own type that derives from Exception."

    This blog entry also has some useful guidelines.

    Also, FxCop code analysis defines a list of "do not raise exceptions" as described here. It recommends:

    The following exception types are too general to provide sufficient information to the user:

    • System.Exception
    • System.ApplicationException
    • System.SystemException

    The following exception types are reserved and should be thrown only by the common language runtime:

    • System.ExecutionEngineException
    • System.IndexOutOfRangeException
    • System.NullReferenceException
    • System.OutOfMemoryException

    So in theory you can raise any other framework exception type, providing you clearly understand the intent of the exception as described by Microsoft (see MSDN documentation).

    Note, these are "guidelines" and as some others have said, there is debate around System.IndexOutOfRangeException (ie many developers throw this exception).

提交回复
热议问题