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

后端 未结 5 883
难免孤独
难免孤独 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条回答
  •  离开以前
    2021-01-30 08:45

    You can create and throw pretty much any of them, but you generally shouldn't. As an example, the various argument validation exceptions (ArgumentException, ArgumentNullException, ArgumentOutOfRangeException, etc) are suitable for use in application code, but AccessViolationException isn't. ApplicationException is provided as a suitable base class for any custom exception classes you may require.

    See this MSDN article for a list of best practices - it refers to handling exceptions, but also contains good advice on creating them...

提交回复
热议问题