What exceptions should be thrown for invalid or unexpected parameters in .NET?

后端 未结 7 759
误落风尘
误落风尘 2020-12-22 16:43

What types of exceptions should be thrown for invalid or unexpected parameters in .NET? When would I choose one instead of another?

Follow-up:

Which excep

7条回答
  •  太阳男子
    2020-12-22 17:12

    ArgumentException:

    ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the parameter specification of the called method. All instances of ArgumentException should carry a meaningful error message describing the invalid argument, as well as the expected range of values for the argument.

    A few subclasses also exist for specific types of invalidity. The link has summaries of the subtypes and when they should apply.

提交回复
热议问题