上面提到的关于 SO 的另一个问题+答案的块不包含适用于此的正确答案!
我有一种用于单元测试的方法。此方法的目的是确保一段代码(由委托引用)将引发特定异常。如果抛出该异常,则单元测试成功。如果没有抛出异常或抛出其他类型的异常,则单元测试将失败。
/// <summary>
/// Checks to make sure that the action throws a exception of type TException.
/// </summary>
/// <typeparam name="TException">The type of exception expected.</typeparam>
/// <param name="action">The code to execute which is expected to generate the exception.</param>
public static void Throws<TException>(Action action)
where TException : Exception
{
try
{
action();
}
catch (TException)
{
return;
}
catch (Exception ex)
{
Assert.Fail("Wrong exception was thrown. Exception of type " + ex.GetType() + " was thrown, exception of type " + typeof(TException) + " was expected.");
}
Assert.Fail("No exception was thrown. Exception of type " + typeof(TException) + " was expected.");
}
下一个调用应该成功,但它失败了:
int result = 0;
Throws<DivideByZeroException>(() => result = result / result);
当TException
抛出预期的类型异常时,它总是被第二个 catch 捕获,而不是第一个 catch。为什么是这样?
当然,我可以使用一个带有一个 catch 的 workarround 并测试 if ex
is of type TException
。我只是想知道/理解为什么这段代码可以编译但很简单(从不?)有效。
编辑
应要求提供“工作”演示:
using System;
namespace GenericExceptionDemo
{
class Program
{
static void Main(string[] args)
{
int n = 0;
Catch<DivideByZeroException>(() => n = n / n);
}
static public void Catch<TException>(Action action)
where TException: Exception
{
try
{
action();
Console.WriteLine("No exception thrown. !!!Fail!!!");
}
catch (TException)
{
Console.WriteLine("Expected exception thrown. PASS!");
}
catch(Exception ex)
{
Console.WriteLine("An unexpected exception of type " + ex.GetType() + " thrown. !!!FAIL!!!");
}
}
}
}