C# Error Handling

Any one know how to monitor excat which part of code getting error(without debug),my programme working fine with most of client machine,but some are getting errors , i wrote programme track to what is the error(straigh going to database )

My code
  catch (Exception PageErr)
ErrMessage ="Page Load -"+PageErr.Message.ToString()+"Source -"+ PageErr.Source.ToString() +"StackTrace -"+ PageErr.StackTrace.ToString();

I need to know what is excact place error generating,
LVL 10
Who is Participating?
Miguel OzSoftware EngineerCommented:
It depends if the name method is unique, then that would be enough. For asp.net I usually add more info like URL page (You can get that from request and it is handy especially when your URL contains query strings) and any essential data that may help you understand the problem.
käµfm³d 👽Commented:
That information should be in stack trace that you are dumping out. The onus is on you to make sure your code is structured in such a way that you can reliably determine exactly where the error is. A stack trace only shows you the inner-most function that raised the error, not which line caused the error. This is one reason to keep your methods smaller rather than larger. With smaller methods you have less code to look at whenever there is an error.
If you mean a line number  - then it is possible if you deploy your code with .pdb files. Code has to be compiled in the Debug mode. Normally no one does that on a number of reasons. Security is one of them.

Without .pdb files you cannot tell the line number. Also, even line number not always tell you exactly what happened. It depends on how code is written.

What can I do  is just to advise to follow good practices and write short methods where code does not spread over three - five screens :). Half a screen, at most - one screen. It will be much easier to localize the problem.
Rose BabuSenior Team ManagerCommented:

by storing full exception details you can find the line number where exactly the exceptions was generated

catch (Exception PageErr)
    ErrMessage ="Error - "+PageErr.ToString().Replace("'", "");
    // Store the exception in DB for the further error findings

Open in new window

Check like this once
käµfm³d 👽Commented:
@Rose Babu

That is only true if you build and deploy under a Debug configuration, as anarki_jimbel stated. In Release mode, the compiler will optimize your code, and so line numbers will no longer match.
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.