将CLR同时处理CLS-投诉和非CLS的投诉例外? [英] Will CLR handle both CLS-Complaint and non-CLS complaint exceptions?

查看:143
本文介绍了将CLR同时处理CLS-投诉和非CLS的投诉例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是我澄清:

我可以在.NET框架都扔CLS-投诉和非CLS的投诉例外?。我正在使用C#3.0。

当我赶上例外

 赶上(异常前)
{}


  • 将它捉只CLS的投诉例外?

  • 有什么用RuntimeWrappedException类(我可以有一个简单的例子?)。


解决方案

  1. 是的,你可以在.NET Framework扔不符合CLS的异常,只要你使用的语言,允许它,例如C ++。

  2. 不,你不能在C#中抛出不符合CLS的异常。

  3. 不,这将捕获所有异常,因为任何不符合CLS的异常将在 RuntimeWrappedException ,从派生异常包裹

  4. 您不扔一个 RuntimeWrappedException ,CLR的一样。 MSDN都有详细的说明,我在这里引用:

    有些语言,如C ++,允许你抛出任何托管类型的异常。其他语言,比如微软的C#和Visual Basic,要求每个抛出的异常从Exception类派生。为了保持语言之间的兼容性,公共语言运行库(CLR)包装不从Exception在RuntimeWrappedException对象派生的对象。


如果您要区别对待CLS和非CLS异常,正好赶上他们在正确的顺序为:

  {尝试
    //一些code
赶上(RuntimeWrappedException前){
    //非CLS异常
赶上(例外前){
    // CLS异常
}

Just for my clarification :

Can i throw both CLS-Complaint and non-CLS complaint exceptions in .NET Framework ?.I am using C# 3.0.

When i catch exception

catch(Exception ex)
{

}

  • Will it catch only CLS-Complaint exceptions?.
  • What is the use of RuntimeWrappedException class (can i have a simple example?).

解决方案

  1. Yes, you can throw non-CLS compliant exceptions in the .NET Framework, as long as you use a language that allows it, e.g. C++.
  2. No, you cannot throw non-CLS compliant exceptions in C#.
  3. No, it will catch all exceptions, since any non-CLS compliant exception will be wrapped in a RuntimeWrappedException, which derives from Exception.
  4. You do not throw a RuntimeWrappedException, the CLR does. MSDN has a detailed description, which I quote here:

    Some languages, such as C++, allow you to throw exceptions of any managed type. Other languages, such as Microsoft C# and Visual Basic, require that every thrown exception be derived from the Exception class. To maintain compatibility between languages, the common language runtime (CLR) wraps objects that do not derive from Exception in a RuntimeWrappedException object.

If you want to treat CLS and non-CLS exceptions differently, just catch them in the right order:

try {
    // some code
catch(RuntimeWrappedException ex) {
    // non-CLS exceptions
catch(Exception ex) {
    // CLS exceptions
}

这篇关于将CLR同时处理CLS-投诉和非CLS的投诉例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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