HOW TO GET ERROR LINE NUMBER IN C#?

jacob rasel

Total Post:88

Points:616
Posted by  jacob rasel
C# 
C#
 7348  View(s)
Ratings:
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!

  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to get error line number in c#?

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!