locked
C# exceptional handling RRS feed

  • Question

  • User276532207 posted

    Hi Everyone,

    I am aware of how to read a line number of the exception in C# exceptional handling (try catch). However, I am wondering if someone provide me a piece of code where I can read the actual string of code which is causing the error.

    for ex: I want the result like this.

    exception line number : line 34

    exception line code : dtEmployee.Rows.Add(dr);

    Wednesday, September 5, 2018 3:48 PM

Answers

  • User276532207 posted

    Here is the piece of code:

    StackTrace st = new StackTrace(ex, true);
    var frame = st.GetFrame(st.FrameCount - 1);
    var lineNumber = frame.GetFileLineNumber();
    var fileName = frame.GetFileName();

    string codebyLineNum = File.ReadLines(fileName).Skip(lineNumber-1).FirstOrDefault();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 6, 2018 8:12 PM

All replies

  • User475983607 posted

    Hi Everyone,

    I am aware of how to read a line number of the exception in C# exceptional handling (try catch). However, I am wondering if someone provide me a piece of code where I can read the actual string of code which is causing the error.

    for ex: I want the result like this.

    exception line number : line 34

    exception line code : dtEmployee.Rows.Add(dr);

    You don't need a piece of code just open the cs file and scroll to the line 34 which contains the code. 

    Perhaps you need to turn on line number in the Visual Studio editor?

    https://docs.microsoft.com/en-us/visualstudio/ide/reference/how-to-display-line-numbers-in-the-editor?view=vs-2017

    Wednesday, September 5, 2018 4:58 PM
  • User276532207 posted

    Thanks for the email. I think its not a big deal. I want to know exact line of code instead going through the 600 lines of code. Hope you understand.

    Wednesday, September 5, 2018 8:48 PM
  • User475983607 posted
    I don’t understand the error tells what line of code. If the exception happened on line 34 then why are you looking through 600 lines?
    Wednesday, September 5, 2018 9:05 PM
  • User-893317190 posted

    Hi asp2018,

    If you mean you want to know the line where  the exception happens, you could use the StackTrace property of  the exception in the catch clause.

    Below is my code. I throw an exception in line 22.

    namespace MyWebFormCases.csharpDemo
    {
        public partial class SplitMethodDemo : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
             
    
    
                try
                {
    
    
    
                    throw new Exception();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.StackTrace);
                  
                }
            }
        }
    }

    And the result is

    at MyWebFormCases.csharpDemo.SplitMethodDemo.Page_Load(Object sender, EventArgs e) in D:\develop\vsworkspace\WebFormCases\MyWebFormCases\csharpDemo\SplitMethodDemo.aspx.cs:line 22

    However, you could only get the line number of the exception. As to the the code causing the exception ,you should check by your own. 

    Best regards,

    Ackerly Xu

    Thursday, September 6, 2018 2:43 AM
  • User1120430333 posted

    Thanks for the email. I think its not a big deal. I want to know exact line of code instead going through the 600 lines of code. Hope you understand.

    exception line number : line 34
    exception line code : dtEmployee.Rows.Add(dr);

    You're being told the line of code and the line number. As stated before, you should find out how to enable Line Numbering in Visual Studio that places a line number on each line of code. There is also a display window that gives the line number and column number the courser is on.

    Thursday, September 6, 2018 5:12 AM
  • User276532207 posted

    I want the result as shown below.

    exception line number : line 34

    exception line code(c#) : dtEmployee.Rows.Add(dr);

    Thursday, September 6, 2018 5:59 PM
  • User475983607 posted

    I want the result as shown below.

    exception line number : line 34

    exception line code(c#) : dtEmployee.Rows.Add(dr);

    As stated above the exception stack trace has this information.

    Thursday, September 6, 2018 6:13 PM
  • User276532207 posted

    Here is the piece of code:

    StackTrace st = new StackTrace(ex, true);
    var frame = st.GetFrame(st.FrameCount - 1);
    var lineNumber = frame.GetFileLineNumber();
    var fileName = frame.GetFileName();

    string codebyLineNum = File.ReadLines(fileName).Skip(lineNumber-1).FirstOrDefault();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 6, 2018 8:12 PM