Home > DeveloperSection > Forums > How to get error line number in c#?
jacob rasel
jacob rasel

Total Post:88

Points:616
Posted on    March-06-2013 4:57 AM

 C# C# 
Ratings:


 1 Reply(s)
 6088  View(s)
Rate this:

Hi!

Can any one help me?

I want to get on which line  exception are generated. Simply I want to stored error line number in C#.

Please help me!

Thanks in advance!



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    March-06-2013 7:53 AM

Hi Jacob!

You can try this way!

Create ExceptionHelper class as below

public static class ExceptionHelper
    {
        public static int LineNumber(this Exception e)
        {

            int linenum = 0;
            try
            {
                linenum = Convert.ToInt32(e.StackTrace.Substring(e.StackTrace.LastIndexOf(":line") + 5));
            }
            catch
            {
                //Stack trace is not available!
            }
            return linenum;
        }
    }

get error line number as below

try
{
// do somthing here
}
catch(Exception ex)
{
int eln = ex.LineNumber();
}

I hope it resolve your problem


Don't want to miss updates? Please click the below button!

Follow MindStick