Getting error line nuber in ASP.Net - C#

Hello,

How do I get the exact line number that has resulted in an exception?
Regards,
MI
vj_miAsked:
Who is Participating?
 
abelConnect With a Mentor Commented:
Sorry, my mistake, I answered too quickly. Here's a sample code in C# that does what you want:

try
{
    // Some code that can cause an exception.
 
    throw new Exception("An error has happened");
}
catch (Exception ex)
{
    System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
 
    MessageBox.Show(trace.GetFrame(0).GetMethod().Name);
    MessageBox.Show("Line: " + trace.GetFrame(0).GetFileLineNumber());
    MessageBox.Show("Column: " + trace.GetFrame(0).GetFileColumnNumber());
}

Open in new window

0
 
abelCommented:
You don't. You can use the .Stacktrace to find it, and the .Source to know the right file. There are tools that fill this gap, though.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.