代码之家  ›  专栏  ›  技术社区  ›  jyoungdev Thilo

捕获C#2.0中作为类型参数给出的异常类型

  •  2
  • jyoungdev Thilo  · 技术社区  · 16 年前

        /// <summary>
        /// asserts that running the command given throws an exception.
        /// </summary>
        public static void Throws<ExceptionType>(ICommand cmd)
            where ExceptionType : Exception
        {
            // Strangely, using 2 catch blocks on the code below makes the first catch block do nothing.
            try
            {
                try
                {
                    cmd.Execute();
                }
                catch (ExceptionType)
                {
                    return;
                }
            }
            catch (Exception f)
            {
                throw new AssertionException(cmd.ToString() + " threw an exception of type " + f.GetType() + ".  Expected type was " + typeof(ExceptionType).Name + ".");
            }
    
            throw new AssertionException(cmd.ToString() + " failed to throw a " + typeof(ExceptionType).Name + ".");
        }
    

        [Test]
        public void Test_Throws_CatchesSpecifiedException()
        {
            AssertThat.Throws<AssertionException>(
                new FailureCommand()
            );
        }
    

    class FailureCommand : ICommand
    {
        public object Execute()
        {
            Assert.Fail();
            return null;    // never reached.
        }
    
        public override string ToString()
        {
            return "FailureCommand";
        }
    }
    

    在NUnit中提供以下输出:

    TestUtil.Tests.AssertThatTests.Test_抛出捕获指定异常: FailureCommand引发了NUnit.Framework.AssertionException类型的异常。预期类型为AssertionException。

    我还尝试使用2个catch块来创建1个try块(而不是在外部try中嵌套try/catch),但得到了相同的结果。

    3 回复  |  直到 16 年前
        1
  •  3
  •   Jon Skeet    16 年前

    在这项测试中,我感觉很好:

    using System;
    using System.IO;
    
    class Test
    {
        static void Main()
        {
            Throws<ArgumentNullException>(() => File.OpenText(null));
        }
    
        public static void Throws<ExceptionType>(Action cmd)
            where ExceptionType : Exception
        {
            try
            {
                try
                {
                    cmd();
                }
                catch (ExceptionType)
                {
                    Console.WriteLine("Caught!");
                    return;
                }
            }
            catch (Exception f)
            {
                Console.WriteLine("Threw an exception of type " + f.GetType() 
                    + ".  Expected type was " + typeof(ExceptionType) + ".");
            }
    
            Console.WriteLine("No exception thrown");
        }
    }
    

    你有多确定这两个 AssertionException 例外情况相同吗?它们是否确实在同一名称空间中(打印) typeof(ExceptionType)

    请尝试一个例外,而不是 让生活更简单。

        3
  •  0
  •   sidney.andrews    16 年前

    我没有调试这段代码,也没有看太久,但可能是您的第一个“return”语句导致代码停止并离开该方法。因此,异常被“捕获”或“处理”(不管您怎么看),外部try/catch块永远不会执行。

    你想这么做吗?

    try        
    {            
        try            
        {                
            cmd.Execute();            
        }            
        catch (ExceptionType)            
        {                
            throw;
        }        
    }        
    catch (Exception f)        
    {            
        throw new AssertionException(cmd.ToString() + " threw an exception of type " + f.GetType() + ".  Expected type was "  + typeof(ExceptionType).Name + ".");        
    }
    
    推荐文章