为什么我不能在C#中捕获一个通用异常? [英] Why can't I catch a generic exception in C#?

查看:144
本文介绍了为什么我不能在C#中捕获一个通用异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对代码进行一些单元测试,这可能会根据输入引起一些异常。所以我尝试像下面的代码:(简单的例子)

  static void Main(string [] args)
{
RunTest< ArgumentException>();
}

static void RunTest< T>()其中T:Exception,new()
{
try
{
throw new T();
// throw new ArgumentException(); < - 不起作用

}
catch(T tex)
{
Console.WriteLine(捕获在异常类型中传递);
}
catch(Exception ex)
{
Console.WriteLine(Caught general exception);
}
Console.Read();
}

但是,这总是打印出抓到一般异常, catch(T tex)处理程序将永远不会工作。无论我是抛出T()还是显式抛出ArgumentException()。任何想法为什么呢?其实我很惊讶,我甚至可以在catch子句中使用T,但是由于这可能不应该这样吗?或者至少给出一个编译器警告/错误,指出这个处理程序将永远不会工作?



我的环境是Visual Studio 2008和3.5是目标框架。

更新:我现在直接从命令提示符尝试它,然后打印出捕获异常类型传递。所以看起来这只限于在Visual Studio中运行。可能是Visual Studio托管过程的一个特点?

解决方案

这里奇怪的行为...



VS2k8控制台应用程序以下内容:

  try 
{
throw new T();
}
catch(T tex)
{
Console.WriteLine(捕获异常类型);
}
catch(Exception ex)
{
Console.WriteLine(Caught general exception);
}

结果捕获一般异常 / p>

但是,从catch语句中删除(无用的)变量:

 尝试
{
throw new T();
}
catch(T)
{
Console.WriteLine(捕获异常类型);
}
catch(异常)
{
Console.WriteLine(Caught general exception);
}

导致异常类型中传递 !!!






更新



Heheh ...它的一个错误: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=362422&wa=wsignin1.0



来源?这里。 为什么catch(TException)处理块行为不同安装Visual Studio 2008之前的调试器?


I was doing some unit testing on code that could throw a number of exceptions depending on the inputs. So I tried something like the below code: (simplified for the example)

    static void Main(string[] args)
    {
        RunTest<ArgumentException>();
    }

    static void RunTest<T>() where T : Exception, new()
    {
        try
        {
            throw new T();
            //throw new ArgumentException(); <-- Doesn't work either

        }
        catch (T tex)
        {
            Console.WriteLine("Caught passed in exception type");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Caught general exception");
        }
        Console.Read();
    }

But this will always print out "Caught general exception", the catch(T tex) handler will never work. It doesn't matter whether I throw T() or explicitly throw ArgumentException(). Any ideas why this is? Actually I was kind of surprised that I was even able to use T in the catch clause, but since that's possible shouldn't this work? Or at least give a compiler warning/error that says that this handler will never work?

My environment is Visual Studio 2008 and 3.5 is the target framework.

UPDATE: I tried it now directly from the command prompt and then it prints out "Caught passed in exception type". So it looks like this is restricted to running from within Visual Studio. Maybe a peculiarity of the Visual Studio hosting process?

解决方案

Bizarre behavior here...

VS2k8 console app. The following:

try
{
    throw new T();
}
catch (T tex)
{
    Console.WriteLine("Caught passed in exception type");
}
catch (Exception ex)
{
    Console.WriteLine("Caught general exception");
}

results in "Caught general exception".

But, remove the (useless) variables from the catch statements:

try
{
    throw new T();
}
catch (T)
{
    Console.WriteLine("Caught passed in exception type");
}
catch (Exception)
{
    Console.WriteLine("Caught general exception");
}

results in "Caught passed in exception type"!!!


Update:

Heheh... Its a bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=362422&wa=wsignin1.0

Source? Here. Why does catch(TException) handling block behaviour differ under the debugger after installing Visual Studio 2008?

这篇关于为什么我不能在C#中捕获一个通用异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆