In C#, are there any built-in exceptions I shouldn't use?

前端 未结 6 2799
[愿得一人]
[愿得一人] 2021-02-20 10:08

Are there any Exceptions defined in the .NET Framework that I shouldn\'t throw in my own code, or that it is bad practice to? Should I write my own?

6条回答
  •  时光说笑
    2021-02-20 10:32

    Microsoft was at one point telling programmers not to inherit directly from Exception, but instead to use ApplicationException as their base class. Not sure if that opinion still holds true, though...

    And if a pre-defined exception already exists that covers your exact error condition (like "NullReferenceException", or "InvalidArgumentException" etc.) - by all means, throw those instead of re-inventing them inside your own code.

    Marc

提交回复
热议问题