How can I get the line number which threw exception?

后端 未结 12 2142
既然无缘
既然无缘 2020-11-27 09:14

In a catch block, how can I get the line number which threw an exception?

相关标签:
12条回答
  • 2020-11-27 10:04

    If you don't have the .PBO file:

    C#

    public int GetLineNumber(Exception ex)
    {
        var lineNumber = 0;
        const string lineSearch = ":line ";
        var index = ex.StackTrace.LastIndexOf(lineSearch);
        if (index != -1)
        {
            var lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length);
            if (int.TryParse(lineNumberText, out lineNumber))
            {
            }
        }
        return lineNumber;
    }
    

    Vb.net

    Public Function GetLineNumber(ByVal ex As Exception)
        Dim lineNumber As Int32 = 0
        Const lineSearch As String = ":line "
        Dim index = ex.StackTrace.LastIndexOf(lineSearch)
        If index <> -1 Then
            Dim lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length)
            If Int32.TryParse(lineNumberText, lineNumber) Then
            End If
        End If
        Return lineNumber
    End Function
    

    Or as an extentions on the Exception class

    public static class MyExtensions
    {
        public static int LineNumber(this Exception ex)
        {
            var lineNumber = 0;
            const string lineSearch = ":line ";
            var index = ex.StackTrace.LastIndexOf(lineSearch);
            if (index != -1)
            {
                var lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length);
                if (int.TryParse(lineNumberText, out lineNumber))
                {
                }
            }
            return lineNumber;
        }
    }   
    
    0 讨论(0)
  • 2020-11-27 10:04

    You could include .PDB symbol files associated to the assembly which contain metadata information and when an exception is thrown it will contain full information in the stacktrace of where this exception originated. It will contain line numbers of each method in the stack.

    0 讨论(0)
  • 2020-11-27 10:06

    Update to the answer

        // 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(st.FrameCount-1);
        // Get the line number from the stack frame
        var line = frame.GetFileLineNumber();
    
    0 讨论(0)
  • 2020-11-27 10:11

    In Global.resx file there is an event called Application_Error

    it fires whenever an error occurs,,you can easily get any information about the error,and send it to a bug tracking e-mail.

    Also i think all u need to do is to compile the global.resx and add its dll's (2 dlls) to your bin folder and it will work!

    0 讨论(0)
  • 2020-11-27 10:14

    If you need the line number for more than just the formatted stack trace you get from Exception.StackTrace, you can use the StackTrace class:

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

    Note that this will only work if there is a pdb file available for the assembly.

    0 讨论(0)
  • 2020-11-27 10:17

    I added an extension to Exception which returns the line, column, method, filename and message:

    public static class Extensions
    {
        public static string ExceptionInfo(this Exception exception)
        {
    
            StackFrame stackFrame = (new StackTrace(exception, true)).GetFrame(0);
            return string.Format("At line {0} column {1} in {2}: {3} {4}{3}{5}  ",
               stackFrame.GetFileLineNumber(), stackFrame.GetFileColumnNumber(),
               stackFrame.GetMethod(), Environment.NewLine, stackFrame.GetFileName(),
               exception.Message);
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题