Get error line number which threw exception using Try Catch in ASP.Net

sureshMGR
 
on Oct 05, 2020 02:33 AM
Sample_113331.zip
1204 Views

Hello friends,

how to get exact line number which exception occurs inside catch?

i tried below code but it alsways showing line number as  0

pls help 

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

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 05, 2020 02:41 AM

Hi sureshMGR,

Check this example. Now please take its reference and correct your code.

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        int line1 = 1;
        int line2 = int.Parse("Test");
    }
    catch (Exception ex)
    {
        var st = new System.Diagnostics.StackTrace(ex, true);
        var frame = st.GetFrame(st.FrameCount - 1);
        var linenumber = frame.GetFileLineNumber();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Try
        Dim line1 As Integer = 1
        Dim line2 As Integer = Integer.Parse("Test")
    Catch ex As Exception
        Dim st = New Diagnostics.StackTrace(ex, True)
        Dim frame = st.GetFrame(st.FrameCount - 1)
        Dim linenumber = frame.GetFileLineNumber()
    End Try
End Sub

Screenshot