Topic:   How to get exact line number which exception occurs inside catch?
Oct 23, 2020 06:12 1 Replies 626 Views SWEETY

1

2

3

4

5

6

7

8

9

10

11

try

{

    int line1=1;

    int line2=int.Parse("Test");

}

catch (Exception ex)

{

    var st = new StackTrace(ex, true);

    var frame = st.GetFrame(0);

    var linenumber = frame.GetFileLineNumber();

}

Prev Next
Topic Replies (1)
  1. 1
    idnkx user

    PARTH

    You can use the StackTrace class:

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

Leave a Reply
Guest User

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect