扔VS重新抛出:同样的结果? [英] Throw VS rethrow : same result?

查看:156
本文介绍了扔VS重新抛出:同样的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指的是在网络上大量的文档资料,特别是SO,例如:的 http://stackoverflow.com/questions/178456/what-is-the-proper-way-to-re-throw-an-exception-in-c
应该有之间的差异扔ê;以及抛;

refering to a lot of documentation on the net, particularly on SO, eg : http://stackoverflow.com/questions/178456/what-is-the-proper-way-to-re-throw-an-exception-in-c there should be a difference between "throw e;" and "throw;".

不过,来自:的 http://bartdesmet.net/blogs/bart/archive/2006/03/12/3815.aspx

这样的代码:

using System;

class Ex
{
   public static void Main()
  {
  //
  // First test rethrowing the caught exception variable.
  //
  Console.WriteLine("First test");
  try
  {
     ThrowWithVariable();
  }
  catch (Exception ex)
  {
     Console.WriteLine(ex.StackTrace);
  }

  //
  // Second test performing a blind rethrow.
  //
  Console.WriteLine("Second test");
  try
  {
     ThrowWithoutVariable();
  }
  catch (Exception ex)
  {
     Console.WriteLine(ex.StackTrace);
  }
}

 private static void BadGuy()
 {
   //
   // Some nasty behavior.
  //
   throw new Exception();
 }

   private static void ThrowWithVariable()
 {
   try
   {
         BadGuy();
   }
  catch (Exception ex)
  {
     throw ex;
  }
}

   private static void ThrowWithoutVariable()
{
  try
  {
     BadGuy();
  }
  catch
  {
     throw;
  }
   }
}



提供了以下的结果:

gives the following result :

$ /cygdrive/c/Windows/Microsoft.NET/Framework/v4.0.30319/csc.exe Test.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.

$ ./Test.exe
First test
   at Ex.ThrowWithVariable()
   at Ex.Main()
Second test
   at Ex.ThrowWithoutVariable()
   at Ex.Main()

这是完全矛盾

which is in complete contradiction with the blog post.

同样的结果与代码获得/rethrowing-exception-without-resetting.html相对=nofollow> http://crazorsharp.blogspot.com/2009/08/rethrowing-exception-without-resetting.html

The same kind of result is obtained with the code from : http://crazorsharp.blogspot.com/2009/08/rethrowing-exception-without-resetting.html

原题:我在做什么错

更新:相同与.net 3.5结果/ CSC.EXE 3.5.30729.4926

UPDATE : same result with .Net 3.5 / csc.exe 3.5.30729.4926

SUMUP :你的答案是伟大的,再次感谢

SUMUP : all your answers were great, thanks again.

所以,原因是有效的内联由于64位抖动。

So the reason is effectively inlining due to the 64-bit JITter.

我不得不选择只有一个答案,这里是为什么我已经选择的 LukeH 的答案:

I had to choose only one answer, and here is why I have choosen LukeH answer :


  • 他猜到了内联的问题,它可能是事实,与我的64位架构,

  • he guessed the inlining problem and the fact it may be related to my 64-bit architecture,

他提供的NoInlining标志,是为了避免这种情况最简单的方法。

he provided the NoInlining flag which is the simplest way to avoid this behavior.

不过这个问题现在上升了另一个问题:是这种行为符合所有的.Net产品规格:CLR 1和C#编程语言的呢?

However this issue now rises another question : is this behavior compliant with all the .Net specifications : the CLR ones and the C# programming language ones ?

更新:此优化似乎符合根据:的 http://stackoverflow.com/questions/3552125/throw-vs-rethrow-same-result/3552244#3552244 (感谢的 0xA3执行的)

UPDATE : this optimization seems compliant according to : http://stackoverflow.com/questions/3552125/throw-vs-rethrow-same-result/3552244#3552244 (thanks 0xA3)

感谢。

推荐答案

我不能复制的问题 - 使用.NET 3.5(32位)为我提供了巴特的文章描述了同样的结果。

I can't replicate the problem -- using .NET 3.5 (32-bit) gives me the same results described in Bart's article.

我的猜测是。在.NET 4编译器/抖动 - 或许这是64位编译器/抖动,如果这是在3.5太发生 - 被内联 BadGuy仅仅方法到调用方法。尝试添加以下 MethodImpl 属性 BadGuy仅仅,看看是否有什么差别:

My guess is that the .NET 4 compiler/jitter -- or maybe it's the 64-bit compiler/jitter if this is happening under 3.5 too -- is inlining the BadGuy method into the calling methods. Try adding the following MethodImpl attribute to BadGuy and see if that makes any difference:

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private static void BadGuy()
{
    //
    // Some nasty behavior.
    //
    throw new Exception();
}

这篇关于扔VS重新抛出:同样的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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