C# 两个例外中的哪一个被称为?

C# 两个例外中的哪一个被称为?,c#,.net,asp.net,exception-handling,C#,.net,Asp.net,Exception Handling,如果我有一个例程可以在两个地方抛出ArgumentException,比如 if (Var1 == null) { throw new ArgumentException ("Var1 is null, this cannot be!"); } if (Val2 == null) { throw new ArgumentException ("Var2 is null, this cannot be either!"); } 在我的调用过程中,确定抛出了两个异常中的哪一个的

如果我有一个例程可以在两个地方抛出ArgumentException,比如

if (Var1 == null)
{
    throw new ArgumentException ("Var1 is null, this cannot be!");
}

if (Val2  == null)
{
    throw new ArgumentException ("Var2 is null, this cannot be either!");
}
在我的调用过程中,确定抛出了两个异常中的哪一个的最佳方法是什么


我是否以错误的方式执行此操作?

您的调用函数不应该关心是哪一行导致了异常。在任何一种情况下,都会抛出一个
ArgumentException
,并且两者的处理方式都应该相同。

将第二个参数中的变量名(Val1、Val2等)传递给ArgumentException构造函数。这将成为ArgumentException.ParamName属性。

对于此特定场景,您应该使用
ArgumentNullException
并正确填充它的
ParamName
属性,以便知道参数为空

ArgumentException
也支持相同的属性,但应使用可用的最具体的异常类型

使用这些类型的异常时,在使用同时接受消息和参数名的构造函数时也要小心。订单在每个异常类型之间切换:

throw new ArgumentException("message", "paramName");

throw new ArgumentNullException("paramName", "message");
控制台中的输出将告诉您输入的消息。

使用构造函数定义哪个参数为null

if (Var1 == null) {
  throw new ArgumentException ("Var1 is null, this cannot be!","Var1");
}

if (Val2  == null){
  throw new ArgumentException ("Var2 is null, this cannot be either!","Var2");
}

对于ArgumentException,您有一个参数,参数有问题:

throw new ArgumentException("Var1 is null, this cannot be!", "Var1");
在更一般的意义上,您通常会使用不同的(可能是自定义的)异常类型,然后调用代码可以有不同的catch块

public class MyCustomException1 : ApplicationException {}
public class MyCustomException2 : ApplicationException {}


try
{
 DoSomething();
}
catch(MyCustomException1 mce1)
{
}
catch(MyCustomException2 mce2)
{
}
catch(Exception ex)
{
}

你应该问自己一个更大的问题是为什么?如果您试图从中引出某种逻辑,那么异常通常是一种不好的方式

相反,您应该有一个返回类型(或out/ref参数),该返回类型将使用某种类型的标志/值进行设置,您可以从调用代码中检测到该标志/值,以确定错误是什么,并将您的逻辑从中删除


如果您坚持使用异常,那么在本例中。您可以抛出异常,然后在捕获异常时,访问以确定导致异常的参数的名称。

当抛出
ArgumentExceptions
时,您可以始终包括导致异常的参数的名称(它的名称)。当然,我假设您真的想知道哪一个为空,在这种情况下,您可能应该使用。

如果这是一个测试用例,您想确保显示正确的异常消息,我知道NUnit在
ExpectedException
属性中有一个关键字。否则,
ArgumentNullException
就是
ArgumentNullException
,应用程序应该对它们一视同仁。如果您想了解更多详细信息,请创建自己的异常类并使用它们

因此,您可以测试以下各项:

[ExpectedException(typeof(ArgumentNullException), ExpectedMessage="Var1 is null, this cannot be!"]
public void TestCaseOne {
    ...
}

[ExpectedException(typeof(ArgumentNullException), ExpectedMessage="Var2 is null, this cannot be either!"]
public void TestCaseTwo {
    ...
}

你没有提供足够的信息来回答你的问题。显而易见的答案是查看异常的消息,但我猜这不是您想要的

如果能够以编程方式区分它们确实很重要,那么请使用另一个异常,或者至少使用当前异常构造函数的
paramName
属性。这会给你一些更相关的信息


但是,使用您自己的异常类型是确保捕获特定情况下的异常的唯一方法。因为
ArgumentException
是框架的一部分,所以您调用的其他东西可能会抛出它,这将导致您进入相同的catch块。如果您创建自己的异常类型(两种方案各有一种),这将为您提供处理特定错误的方法。当然,从你的例子来看,在调用函数之前,只需检查
Val1
Val2
是否为null似乎更简单。

+1在调用该方法之前,也可以轻松检查这一点。但是如果我想根据引发的两个异常中的哪一个向用户显示特定消息,您可以使用异常本身的文本,或者改为抛出
ArgumentNullException
。在这种情况下,ArgumentException的第二个参数应该是字符串,即有问题的参数的名称。
[ExpectedException(typeof(ArgumentNullException), ExpectedMessage="Var1 is null, this cannot be!"]
public void TestCaseOne {
    ...
}

[ExpectedException(typeof(ArgumentNullException), ExpectedMessage="Var2 is null, this cannot be either!"]
public void TestCaseTwo {
    ...
}