Why must a type thrown or caught derive from System.Exception

前端 未结 6 1010
独厮守ぢ
独厮守ぢ 2021-02-12 09:44

So just out of curiosity I wanted to see what was special about the exception class that allowed it to be used with the keyword Throw while a standard class is not.

6条回答
  •  北恋
    北恋 (楼主)
    2021-02-12 10:05

    Design Guidelines for Exceptions

    Exceptions are the standard mechanism for reporting errors. Applications and libraries should not use return codes to communicate errors. The use of exceptions adds to a consistent framework design and allows error reporting from members, such as constructors, that cannot have a return type

    throw (C# Reference)

    The thrown exception is an object whose class is derived from System.Exception, as shown in the following example.

    class MyException : System.Exception {}
    // ...
    throw new MyException();
    

    Exceptions Overview

    In the .NET Framework, an exception is an object that inherits from the Exception Class

    So, your exception must derive from System.Exception, but it's up to you, how you organize it within.

提交回复
热议问题