异常的堆栈跟踪不显示在异常被抛出 [英] Exception's stacktrace doesn't show where the exception was thrown

查看:862
本文介绍了异常的堆栈跟踪不显示在异常被抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常情况下,当我抛出一个异常,抓住它,并打印出堆栈跟踪,我能看到那里的异常被抛出的电话,导致呼叫,导致的是,等回到整个程序的根目录。

Typically when I throw an exception, catch it, and print out the stacktrace, I get to see the call where the exception was thrown, the call that led to that, the call that led to that, and so on back to the root of the entire program.

现在它只是显示我在异常正在的的电话,不是哪里它被的抛出的。我想不出有什么变化导致了这一点。下面是我的程序:

Now it's only showing me the call where the exception is being caught, not where it's being thrown. I can't figure out what's changed to lead to this. Here's my program:

using System;

class foo {
    static void Main(string[] args) {
        try { f(); }
        catch (Exception e) { Console.WriteLine(e.StackTrace); }
    }
    static void f() { g(); }
    static void g() { throw new Exception(); }
}

和这里是被打印出来:

at foo.Main(String[] args) in C:\Projects\test\root.cs:line 5

我希望会是这样的:

at foo.g...
at foo.f...
at foo.Main...

任何想法?

推荐答案

它仍然显示了异常被抛出,没有它被抓住了。但由于优化,可能不是,你希望它被抛出。

It still shows where the exception was thrown, not where it was caught. But thanks to optimizations that might not be where you expect it to be thrown.

最有可能抛出异常的函数被内联到调用函数。

Most likely the function that throws the exception was inlined into the calling function.

在这种情况下,我希望JIT优化来负责。默认情况下,在调试器中运行时,但优化不优化,因此内联时,在调试器中不运行的方法。我不知道,如果调试版本有内联的效果。

In this case I expect the JIT optimizer to be responsible. By default it doesn't optimize when running in a debugger, but optimizes and thus inlines the method when not running in a debugger. I'm not sure if a Debug build has an effect on inlining.

如果您添加 [MethodImpl(MethodImplOptions.NoInlining)] 给一个函数不会被内联和您的堆栈跟踪应该是符合市场预期。

If you add [MethodImpl(MethodImplOptions.NoInlining)] to a function it won't be inlined and your stack-trace should be as expected.

这篇关于异常的堆栈跟踪不显示在异常被抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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