我是否需要创建自己的 InvalidArgumentException .. 我在 c# 中找不到任何内置类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/481261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:07:11  来源:igfitidea点击:

Do I need to create my own InvalidArgumentException.. I couldn't find any builtin type in c#

c#exception

提问by StackUnderflow

Do I need to create my own InvalidArgumentException.. I couldn't find any built-in types in c#... Is there any library which defines commonly used Exception classes.. Thanks

我是否需要创建自己的 InvalidArgumentException .. 我在 c# 中找不到任何内置类型......是否有任何定义常用异常类的库..谢谢

采纳答案by Vojislav Stojkovic

It's called ArgumentExceptionand it's in the built-in System namespace.

它称为ArgumentException,位于内置 System 命名空间中。

回答by andrew.fox

Besides ArgumentExceptionthere are also ArgumentNullExceptionand ArgumentOutOfRangeException.

此外ArgumentException还有ArgumentNullExceptionArgumentOutOfRangeException

Short summary;

简短的摘要;

  • System.ArgumentException- general exception,
  • System.ArgumentNullException- unexpected null value,
  • System.ArgumentOutOfRangeException- unexpected value.
  • System.ArgumentException- 一般例外,
  • System.ArgumentNullException- 意外的空值,
  • System.ArgumentOutOfRangeException- 意想不到的价值。

Standard usage:

标准用法:

void GetEntity(string currentName)
{
  if (String.IsNullOrEmpty(currentName))
  {
    throw new ArgumentNullException(nameof(currentName));
  }

  //...
}

PS. NotSupportedExceptionalso comes in handy (e.g. when the set of arguments would force the method to perform not supported scenario).

附注。 NotSupportedException也派上用场(例如,当参数集会强制方法执行不受支持的场景时)。