确定哪个代码行引发了异常

发布于 2024-07-26 11:12:26 字数 72 浏览 5 评论 0原文

在 dotNet 中,一行抛出异常并被捕获,我如何找出哪个文件中的哪一行抛出了异常? 看起来比较简单,但我无法弄清楚......

In dotNet a line throws an exception and is caught, how can I figure out which line in which file threw the exception? Seems relatively straightforward, but I can't figure it out...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

近箐 2024-08-02 11:12:26

仅当有可用的调试符号时才可以执行此操作。

catch(Exception ex) {
    // check the ex.StackTrace property
}

如果你想在 VS 中调试这种情况,你最好只在位于 Exceptions 对话框中选中 Common Language Runtime ExceptionsThrown 复选框。 调试菜单。 一旦抛出异常,调试器就会中断,即使它位于 try 块中。

You can only do it if you have debug symbols available.

catch(Exception ex) {
    // check the ex.StackTrace property
}

If you want to debug such a situation in VS, you'd better just check Thrown checkbox for Common Language Runtime Exceptions in Exceptions dialog located in Debug menu. The debugger will break as soon as the exception is thrown, even if it's in a try block.

梦境 2024-08-02 11:12:26

就我个人而言,我只是记录异常的 ToString() 返回值。 包括整个堆栈跟踪。 这是一行代码......非常简单。

Personally, I just log the exception's ToString() return value. The whole stack trace is included. It's one line of code ... dead simple.

情深缘浅 2024-08-02 11:12:26

您可以使用 StackFrame 类

try
{
    ...
    ...

}
catch(...)
{
    StackFrame sf = new StackFrame(true);

    int lineNumber = sf.GetFileLineNumber();
    int colNumber = sf.GetFileColumnNumber();
    string fileName = sf.GetFileName();
    string methodName = sf.GetMethod().Name;
}

You could use the StackFrame Class:

try
{
    ...
    ...

}
catch(...)
{
    StackFrame sf = new StackFrame(true);

    int lineNumber = sf.GetFileLineNumber();
    int colNumber = sf.GetFileColumnNumber();
    string fileName = sf.GetFileName();
    string methodName = sf.GetMethod().Name;
}
千と千尋 2024-08-02 11:12:26

嗯,在 .NET 中,有所谓的 FirstChanceException。 这些本质上是在处理异常之前抛出的。 有两种方式来看待您在此提出的问题。 一是从调试角度。 如果进行调试,您只需将调试器设置为从“调试/异常”窗口捕获抛出的异常。 这在交互式环境中更容易。 如果您需要在非交互式上下文中记录此信息,那么我会执行类似于 CMS 正在讨论的操作...

try
{
    ...
}
catch(Exception ex)
{
    System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(ex);
    System.Diagnostics.StackFrame firstFrame = stackTrace.GetFrame[0];
    Console.WriteLine(firstFrame.GetFileLineNumber);
    ...
}

这里唯一的区别是我们获取整个堆栈跟踪,然后转到第一帧,其中是最初抛出异常的地方。

Well, in .NET you have whats called a FirstChanceException. These essentially are thrown before an exception is handled. There are two ways of looking at the issue that you're presenting here. One is from a debugging angle. If debugging you can just set your debugger to catch thrown exceptions from the Debug/Exceptions window. This is easier in an interactive context. IF you need to record this information from within a non-interactive context then I would do something similar to what CMS is talking about...

try
{
    ...
}
catch(Exception ex)
{
    System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(ex);
    System.Diagnostics.StackFrame firstFrame = stackTrace.GetFrame[0];
    Console.WriteLine(firstFrame.GetFileLineNumber);
    ...
}

The only difference here is that we get the entire Stack Trace, then go to the first frame, which is where the exception was originally thrown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文