Throw VS rethrow : same result?

前端 未结 5 1714
隐瞒了意图╮
隐瞒了意图╮ 2021-02-20 11:26

refering to a lot of documentation on the net, particularly on SO, eg : What is the proper way to re-throw an exception in C#? there should be a difference between \"throw e;\"

5条回答
  •  独厮守ぢ
    2021-02-20 12:02

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

    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();
    }
    

提交回复
热议问题