ArgumentException 和 Exception 之间有什么区别?
What is the difference between ArgumentException and just Exception?
在我们教授的示例代码中,他有一个片段如下所示:
if (name == null || name == "")
throw new ArgumentException("name is null or empty");
还有一个看起来像这样的片段:
if (!File.Exists(name))
{
throw new Exception("File does not exist!");
}
我只是想知道有什么不同,为什么一个比另一个高
Exception
is the base class for all exceptions. ArgumentException
用于表示参数无效。它是 Exception
的子类。使用 catch
,您实际上可以根据异常类型进行过滤并以不同方式处理每一个。
MSDN 描述得很好:
When you have to throw an exception, you can often use an existing exception type in the .NET Framework instead of implementing a custom exception. You should use a standard exception type under these two conditions:
- You are throwing an exception that is caused by a usage error (that is, by an error in program logic made by the developer who is calling your method). Typically, you would throw an exception such as ArgumentException, ArgumentNullException, InvalidOperationException, or NotSupportedException. The string you supply to the exception object's constructor when instantiating the exception object should describe the error so that the developer can fix it. For more information, see the Message property.
- You are handling an error that can be communicated to the caller with an existing .NET Framework exception. You should throw the most derived exception possible. For example, if a method requires an argument to be a valid member of an enumeration type, you should throw an InvalidEnumArgumentException (the most derived class) rather than an ArgumentException.
Exception
是基数 class。它只是最通用的一种异常。在许多情况下,可以使用更具体的类型来提供有关发生的错误类型的更多信息。在这种情况下,ArgumentException
是一种异常类型,表示参数存在错误。
TLDR:ArgumentException
是一种 Exception
,用于提供更详细的信息
物是万物之本
对象有很多子类型。例外就是其中之一。
异常有很多子类型。 SystemException 就是其中之一。
SystemException 有很多子类型。 ArgumentException 就是其中之一。
ArgumentException 有很多子类型。 ArgumentNullException 和 ArgumentOutOfRangeException 就是其中的两个。
如果你的教授真的想使用他们本可以编写的最明确的异常
if (name==null) throw new ArgumentNullException("name");
if (name=="") throw new ArgumentOutOfRangeException("name", name, "name cannot be zero length");
在我们教授的示例代码中,他有一个片段如下所示:
if (name == null || name == "")
throw new ArgumentException("name is null or empty");
还有一个看起来像这样的片段:
if (!File.Exists(name))
{
throw new Exception("File does not exist!");
}
我只是想知道有什么不同,为什么一个比另一个高
Exception
is the base class for all exceptions. ArgumentException
用于表示参数无效。它是 Exception
的子类。使用 catch
,您实际上可以根据异常类型进行过滤并以不同方式处理每一个。
MSDN 描述得很好:
When you have to throw an exception, you can often use an existing exception type in the .NET Framework instead of implementing a custom exception. You should use a standard exception type under these two conditions:
- You are throwing an exception that is caused by a usage error (that is, by an error in program logic made by the developer who is calling your method). Typically, you would throw an exception such as ArgumentException, ArgumentNullException, InvalidOperationException, or NotSupportedException. The string you supply to the exception object's constructor when instantiating the exception object should describe the error so that the developer can fix it. For more information, see the Message property.
- You are handling an error that can be communicated to the caller with an existing .NET Framework exception. You should throw the most derived exception possible. For example, if a method requires an argument to be a valid member of an enumeration type, you should throw an InvalidEnumArgumentException (the most derived class) rather than an ArgumentException.
Exception
是基数 class。它只是最通用的一种异常。在许多情况下,可以使用更具体的类型来提供有关发生的错误类型的更多信息。在这种情况下,ArgumentException
是一种异常类型,表示参数存在错误。
TLDR:ArgumentException
是一种 Exception
,用于提供更详细的信息
物是万物之本
对象有很多子类型。例外就是其中之一。
异常有很多子类型。 SystemException 就是其中之一。
SystemException 有很多子类型。 ArgumentException 就是其中之一。
ArgumentException 有很多子类型。 ArgumentNullException 和 ArgumentOutOfRangeException 就是其中的两个。
如果你的教授真的想使用他们本可以编写的最明确的异常
if (name==null) throw new ArgumentNullException("name");
if (name=="") throw new ArgumentOutOfRangeException("name", name, "name cannot be zero length");