How to get error line number of code using try-catch

前端 未结 10 942
轮回少年
轮回少年 2020-12-05 13:08

I want to get line number of code which cause error. For example;

static void Main(string[] args)
{
    using (SqlConnection conn = new SqlConnection(bagcum)         


        
相关标签:
10条回答
  • 2020-12-05 13:53

    Copy the entire stack trace in to a string or stringbuilder by using try/catch that can throw, see the below example

    try
    {
        //Do some programming
    }
    catch(Exception ex)
    {
    
       //Catch the exception and assign the stack trace
       StackTrace = ex;
    }
    

    The output will be

    System.IndexOutOfRangeException: Index was outside the bounds of the array.   
    at Program.Run() in C:\Console Application1\Program.cs:line 37    
    at Program.Main(String[] args) in C:\Console Application1\Program.cs:line 45 
    

    The first line shows the type of the exception and the message. The second line shows the file, function and line number where the exception was thrown

    0 讨论(0)
  • 2020-12-05 13:58

    the following code exception log handler method is works fine :

    in catch :

     catch (Exception ex)
                {
                    CommonTools.vAddToLog(ex, EmpID, ErrorCodes.UnDefined);
                    Response.Redirect("~/ErrorPage.aspx");
                }
    

    in AddToLog method :

     string _exMsgErr = string.Empty;
                    var frame = oStackTrace.FrameCount > 1 ? oStackTrace.GetFrame(1) : oStackTrace.GetFrame(0);
                    if (oException.GetType() == typeof(JOVALException))
                    {
                        JOVALException _JOVALEx = (JOVALException)oException;
                        _exMsgErr = _JOVALEx.Message;
                    }
                    else
                    {
                        _exMsgErr = oException.Message;
                    }
                    ErrorLog oError = new ErrorLog(frame.GetMethod().Name, (string)frame.GetFileName(), (int)frame.GetFileLineNumber(), sCustomErrorMessage == string.Empty ? _exMsgErr : sCustomErrorMessage, sUserID, oErrCode);
                    //Cont. your code of log file
    

    Finally the xml log file looks like this :

    <ErrorLog>
    <MethodName>FillRolesDDLs</MethodName>
    <FileName>
    F:\Projects\ERP\ERP\Pages\SystemSettings\Roles.aspx.cs
    </FileName>
    <LineNumber>61</LineNumber>
    <ErrorMesssage>
    The given DataRow is not in the current DataRowCollection.
    </ErrorMesssage>
    <UserID>1</UserID>
    <ErrCode>UnDefined</ErrCode>
    <Time>15/03/2015 16:23:21.976</Time>
    </ErrorLog>
    
    0 讨论(0)
  • 2020-12-05 14:03

    The problem is that you're trying to get the line number of the first frame of the exception:

    System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
    Console.WriteLine("Line: " + trace.GetFrame(0).GetFileLineNumber());
    

    However, the exception does not originate in the line you write ExecuteNonQuery, but somewhere within that function, possibly multiple stack frames (i.e. nested function calls) deeper. So the first frame (which you explicitly retrieve using GetFrame(0)) is somewhere inside Microsoft's code (most likely System.Data.dll) for which you don't have any debugging symbols.

    Write out the complete exception stacktrace in your function to see what I mean:

    try
    {
       // your code ...
    }
    catch (Exception ex) 
    {
       Console.WriteLine(ex);
    }
    

    Short of parsing the stacktrace (i.e. ex.StackTrace) there is no reliable why to get the linenumber of the "ExecuteNonQuery()" invocation. I would especially not try to count the stackframes up the stack where your call to ExecuteNonQuery() happens.

    I wonder however, what you need the sole linenumber for, why not just log/print/whatever the complete stacktrace instead. At least for diagnostics reasons that is much more useful anyway.

    0 讨论(0)
  • 2020-12-05 14:03

    To get line numbers, you need your application to be in Debug mode or include the debug symbols in the same folder (the .pdb file) for line numbers to appear. You code as posted should then work.

    0 讨论(0)
提交回复
热议问题