在异常处理中显示行号

发布于 2024-07-16 10:35:20 字数 218 浏览 11 评论 0原文

如何显示导致错误的行号?这是否可以通过 .NET 编译其 .exe 的方式实现?

如果没有, Exception.Message 是否有一种自动方式来显示崩溃的子程序?

try
{
  int x = textbox1.Text;
}
catch(Exception ex)
{
     MessageBox.Show(ex.Message);
}

How would one display what line number caused the error and is this even possible with the way that .NET compiles its .exes?

If not is there an automated way for Exception.Message to display the sub that crapped out?

try
{
  int x = textbox1.Text;
}
catch(Exception ex)
{
     MessageBox.Show(ex.Message);
}

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

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

发布评论

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

评论(7

怪我闹别瞎闹 2024-07-23 10:35:20

使用 ex.ToString() 获取完整的堆栈跟踪。

即使在发布模式下,您也必须使用调试符号(.pdb 文件)进行编译,才能获取行号(这是项目构建属性中的一个选项)。

Use ex.ToString() to get the full stack trace.

You must compile with debugging symbols (.pdb files), even in release mode, to get the line numbers (this is an option in the project build properties).

懒的傷心 2024-07-23 10:35:20

要查看给定异常的堆栈跟踪,请使用 e.StackTrace

如果您需要更详细的信息,可以使用系统。 Diagnostics.StackTrace 类(这里有一些代码供您尝试):

try
{
    throw new Exception();
}
catch (Exception ex)
{
    //Get a StackTrace object for the exception
    StackTrace st = new StackTrace(ex, true);

    //Get the first stack frame
    StackFrame frame = st.GetFrame(0);

    //Get the file name
    string fileName = frame.GetFileName();

    //Get the method name
    string methodName = frame.GetMethod().Name;

    //Get the line number from the stack frame
    int line = frame.GetFileLineNumber();

    //Get the column number
    int col = frame.GetFileColumnNumber();
}

只有当程序集有可用的 pdb 文件时,这才有效。 查看项目属性 - 构建选项卡 - 高级 - 调试信息选择以确保存在 pdb 文件。

To see the stacktrace for a given Exception, use e.StackTrace

If you need more detailed information, you can use the System.Diagnostics.StackTrace class (here is some code for you to try):

try
{
    throw new Exception();
}
catch (Exception ex)
{
    //Get a StackTrace object for the exception
    StackTrace st = new StackTrace(ex, true);

    //Get the first stack frame
    StackFrame frame = st.GetFrame(0);

    //Get the file name
    string fileName = frame.GetFileName();

    //Get the method name
    string methodName = frame.GetMethod().Name;

    //Get the line number from the stack frame
    int line = frame.GetFileLineNumber();

    //Get the column number
    int col = frame.GetFileColumnNumber();
}

This will only work if there is a pdb file available for the assembly. See the project properties - build tab - Advanced - Debug Info selection to make sure there is a pdb file.

赠我空喜 2024-07-23 10:35:20

如果您使用 'StackTrace' 并包含 . pdb 文件位于工作目录中,堆栈跟踪应包含行号。

If you use 'StackTrace' and include the .pdb files in the working directory, the stack trace should contain line numbers.

錯遇了你 2024-07-23 10:35:20
string lineNumber=e.StackTrace.Substring(e.StackTrace.Length - 7, 7);
string lineNumber=e.StackTrace.Substring(e.StackTrace.Length - 7, 7);
枕花眠 2024-07-23 10:35:20

这样你就可以从异常中获取行号

public int GetLineNumber(Exception ex)
{

    const string lineSearch = ":line ";
    var index = ex.StackTrace.LastIndexOf(lineSearch);
    int ln=0;
    if (index != -1)
    {


        var lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length);
        string lnum = System.Text.RegularExpressions.Regex.Match(lineNumberText, @"\d+").Value;
        int.TryParse(lnum,out ln);

    }
    return ln;
}

this way you can Get Line number from Exception

public int GetLineNumber(Exception ex)
{

    const string lineSearch = ":line ";
    var index = ex.StackTrace.LastIndexOf(lineSearch);
    int ln=0;
    if (index != -1)
    {


        var lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length);
        string lnum = System.Text.RegularExpressions.Regex.Match(lineNumberText, @"\d+").Value;
        int.TryParse(lnum,out ln);

    }
    return ln;
}
皇甫轩 2024-07-23 10:35:20

如果生成异常的库是使用调试符号编译的,则行号将包含在堆栈跟踪中。 这可以是单独的文件 (*.pdb) 或嵌入到库中。

对于 .NET Core、.NET 5 及更高版本,要在发布版本中具有完整的异常行号,请按如下方式配置项目:

<PropertyGroup>    
  <DebugSymbols>true</DebugSymbols>
  <DebugType>embedded</DebugType>

    <!-- Only enable the following if the line numbers mismatch -->
    <!--<Optimize>false</Optimize>-->
    
    <!--
      Additional properties which may impact how printed line numbers match the source code line numbers are listed here:
      https://learn.microsoft.com/en-us/dotnet/core/run-time-config/compilation
    -->
</PropertyGroup>

上述配置将直接在构建文件中包含调试符号,这些文件可以作为 nuget 发布。

上述方法的替代方法是将调试包与主 nuget 包一起恢复,目前尚不支持: https://github.com/NuGet/Home/issues/9667

现在获取异常行号:

try
{
    throw new Exception();
}
catch (Exception ex)
{
    // Get stack trace for the exception with source file information
    var st = new StackTrace(ex, true);
    // Get the top stack frame
    var frame = st.GetFrame(0);
    // Get the line number from the stack frame
    var line = frame.GetFileLineNumber();
}

Line numbers will be included in the stack trace if the library which generated the exception is compiled with debug symbols. This can be a separate file (*.pdb) or embedded in the library.

For .NET Core, .NET 5 and later, to have full exception line numbers in release builds, configure the project as follows:

<PropertyGroup>    
  <DebugSymbols>true</DebugSymbols>
  <DebugType>embedded</DebugType>

    <!-- Only enable the following if the line numbers mismatch -->
    <!--<Optimize>false</Optimize>-->
    
    <!--
      Additional properties which may impact how printed line numbers match the source code line numbers are listed here:
      https://learn.microsoft.com/en-us/dotnet/core/run-time-config/compilation
    -->
</PropertyGroup>

The above configuration will include debug symbols directly with the built files, which can be published as nugets.

An alternative to the above is to restore debug packages together with the main nuget packages, which is currently not yet supported: https://github.com/NuGet/Home/issues/9667

Now get the exception line numbers:

try
{
    throw new Exception();
}
catch (Exception ex)
{
    // Get stack trace for the exception with source file information
    var st = new StackTrace(ex, true);
    // Get the top stack frame
    var frame = st.GetFrame(0);
    // Get the line number from the stack frame
    var line = frame.GetFileLineNumber();
}
回忆追雨的时光 2024-07-23 10:35:20

是否可以简单地从 ex 公开的 StackTrace 中获取顶部框架?

try
{
    throw new Exception();
}
catch (Exception ex)
{
    // Get the top stack frame
    var frame = ex.StackTrace().GetFrame(0);
    // Get the line number from the stack frame
    var line = frame.GetFileLineNumber();
}

Is it possible to simply get the top frame from the StackTrace exposed by ex?

try
{
    throw new Exception();
}
catch (Exception ex)
{
    // Get the top stack frame
    var frame = ex.StackTrace().GetFrame(0);
    // Get the line number from the stack frame
    var line = frame.GetFileLineNumber();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文